简体   繁体   中英

F# XUnit test does not print output

I have the following XUnit test in a project created with dotnet new xunit :

type Scenarios(output : ITestOutputHelper) =

  [<Fact>]
  member __.``Output shows up`` () =
    output.WriteLine("I'm here")

This approach seems to have worked before , but running the tests doesn't show any output, regardless of whether I use dotnet test or dotnet xunit :

> dotnet test
Build started, please wait...
Build completed.

Test run for C:\Work\OSS\Streamstone.fs\tests\StreamstoneFs.Tests\bin\Debug\netcoreapp2.0\StreamstoneFs.Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.6.0-preview-20180109-01
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.4295013]   Discovering: StreamstoneFs.Tests
[xUnit.net 00:00:00.4750480]   Discovered:  StreamstoneFs.Tests
[xUnit.net 00:00:00.4792986]   Starting:    StreamstoneFs.Tests
[xUnit.net 00:00:00.5964013]   Finished:    StreamstoneFs.Tests

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.1835 Seconds

> dotnet xunit
Detecting target frameworks in StreamstoneFs.Tests.fsproj...
Building for framework netcoreapp2.0...
  StreamstoneFs -> C:\Work\OSS\Streamstone.fs\src\StreamstoneFs\bin\Debug\netstandard2.0\StreamstoneFs.dll
  StreamstoneFs.Tests -> C:\Work\OSS\Streamstone.fs\tests\StreamstoneFs.Tests\bin\Debug\netcoreapp2.0\StreamstoneFs.Tests.dll
Running .NET Core 2.0.0 tests for framework netcoreapp2.0...
xUnit.net Console Runner (64-bit .NET Core 4.6.00001.0)
  Discovering: StreamstoneFs.Tests
  Discovered:  StreamstoneFs.Tests
  Starting:    StreamstoneFs.Tests
  Finished:    StreamstoneFs.Tests
=== TEST EXECUTION SUMMARY ===
   StreamstoneFs.Tests  Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.109s

What am I doing wrong here?

I can get the output to show up only if the unit test fails:

type StackOverflow(output : ITestOutputHelper) = 

    [<Fact>]
    member __.``Output shows up`` () =
        output.WriteLine("hello world, from output")
        printfn "Hello world, from printfn!"
        Assert.True(false)

This will give the output of:

Failed   Tests+StackOverflow.Output shows up
Error Message:
 Assert.True() Failure
Expected: True
Actual:   False
Stack Trace:
   at Tests.StackOverflow.Output shows up() in C:\git\dotnetTesting\Tests.fs:line 17
Standard Output Messages:
 hello world, from output

The printfn output never shows up, regardless of test results. If the test passes, then the output never shows up, either.

From the docs , it looks like the output is designed for debugging, so this makes some sense. It looks to me like the dotnet command line tool doesn't capture the output the way visual studio does in their example.

I got output to appear without a failing test by using Console.SetOut

module StackOverflowTests

open System.IO
open System
open Xunit
open Xunit.Abstractions

type Converter(output: ITestOutputHelper) =
    inherit TextWriter()
    override __.Encoding = stdout.Encoding
    override __.WriteLine message =
        output.WriteLine message
    override __.Write message =
        output.WriteLine message

type StackOverflow(output : ITestOutputHelper) =
    do new Converter(output) |> Console.SetOut

    [<Fact>]
    member __.``Output shows up, for real`` () =
        output.WriteLine "ITestOutputHelper"
        printfn "printfn"
        printf "printf"
        Console.WriteLine("Console.WriteLine")

Here's it working in Rider; it also works in Visual Studio 2019. I haven't tested it in VSCode/Ionide. 骑手的截图

My answer combines this and this . I didn't have to do anything to xunit.diagnosticMessages .

这适用于带有.NET Core项目的VS Code,虽然它有点吵:

dotnet test --logger:"console;verbosity=detailed"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM