12

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?

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402

3 Answers3

6

This works in VS Code with a .NET Core project, though it's a bit noisy:

dotnet test --logger:"console;verbosity=detailed"
Scott Hutchinson
  • 1,703
  • 12
  • 22
4

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. Screenshot from rider

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

DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52
  • In VS Code, this does not work. I see the output labeled as "Standard Output Messages:" only if the test failed. – Scott Hutchinson May 29 '19 at 18:04
  • Also, app.config does not work in a .NET Core project. But this works in VS Code, though it's a bit noisy: dotnet test --logger:"console;verbosity=detailed" – Scott Hutchinson May 29 '19 at 18:49
3

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.

Max
  • 849
  • 9
  • 24