40

In Visual Studio 2015 Community I have a sample ASP.NET 5 (vNext) project and a project with unit tests (xUnit.net). The version of DNX is 1.0.0-beta5. My goal is to add messages during the test run to the output pane. Here I took a way to do this, so my unit test code looks like this:

using Xunit;
using Xunit.Abstractions;

namespace UnitTests
{

    public class UnitTest1
    {
        ITestOutputHelper output;

        public UnitTest1(ITestOutputHelper output)
        {
            this.output = output;
        }

        [Fact]
        public void TestTestTest()
        {
            output.WriteLine("Test Message");
            Assert.Equal(2, 2);
        }

    }
}

Visual Studio Test Explorer discovers this test (and that's OK), but all I have in the Output pane (from Tests) is:

------ Run test started ------
------ Test started: Project: UnitTests ------
Starting  Microsoft.Framework.TestHost [C:\Users\*******\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta5\bin\dnx.exe --appbase "C:\Users\*******\Documents\Visual Studio 2015\Projects\MvcMovie\UnitTests" Microsoft.Framework.ApplicationHost --port 55837 Microsoft.Framework.TestHost --port 55893]
Connected to Microsoft.Framework.TestHost
Running tests in 'C:\Users\*******\Documents\Visual Studio 2015\Projects\MvcMovie\UnitTests\project.json'
========== Run test finished: 1 run (0:00:03,2267169) ==========

Also, there is not any link "Output" under the selected test run information, like here: Enter image description here (Only "Test passed... Elapsed time ...")

What should I do to make this ITestOutputHelper work?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Farfi
  • 473
  • 1
  • 4
  • 8

7 Answers7

8

This solution works for me (Visual Studio 2017 and xUnit 2.2.0.3545).

Try adding the below configuration in the App.Config file. (I don't know why. It was just needed. If it doesn't exist, just add a new one in your test project.)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="xunit.diagnosticMessages" value="true"/>
  </appSettings>
</configuration>

The output information will be written in Test output as expected like below.

[xUnit.net 00:00:00.0853907]   Starting:    xxxAssemblyName (parallel test collections = on, max threads = 8)
[xUnit.net 00:00:00.1689373]     Example.xxxTestClassName [PASS]
[xUnit.net 00:00:00.1697265]       Output:
[xUnit.net 00:00:00.1700698]         xxxx your Test Message
[xUnit.net 00:00:00.1797303]   Finished:    xxxAssemblyName
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
  • 1
    **Note:** XML-based config has [deprecated](https://xunit.github.io/docs/configuring-with-xml), if possible use [JSON-based configuration](https://xunit.github.io/docs/configuring-with-json) now. – Matt Mar 27 '19 at 12:27
  • 6
    **xunit.runner.json:** `{ "$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json", "appDomain": "ifAvailable", "diagnosticMessages": true, "internalDiagnosticMessages": false, "longRunningTestSeconds": 4, "maxParallelThreads": 8, "methodDisplay": "classAndMethod", "methodDisplayOptions": "none", "parallelizeAssembly": true, "parallelizeTestCollections": true, "preEnumerateTheories": true, "shadowCopy": true }` – Matt Mar 29 '19 at 10:28
  • Your answer works, thank you!. **n.b.:** I am using the `xunit.runner.json` file above, setting `"diagnosticMessages": true` – Matt Mar 29 '19 at 10:31
  • 1
    Remember to set the file xunit.runner.json to CopyAlways, this got me spinning for a while – thor Sep 04 '19 at 12:03
  • 2
    @TGP1994 - Thanks for the information, yes both links are broken now. But I found new links here: [deprecated XML-based configuration](https://xunit.net/docs/configuring-with-xml) and [JSON-based configuration](https://xunit.net/docs/configuration-files). Hope that helps. – Matt Mar 03 '21 at 07:39
  • Transformed into json, I think it would look like: `{"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", "diagnosticMessages": true}`, name it `xunit.runner.json` and store it at the root directory of your project. – Matt Mar 19 '21 at 10:48
  • If you're using only this answer and it's still not working, note the absolutely vital bit of info in [Stato Machino's answer](https://stackoverflow.com/a/52450579/8417190). Took me a while to notice! You _must_ have a failing test, otherwise it won't output the diagnostic info to the main "Tests" output (but it will still output it to the individual tests in the Test Explorer window). – jsabrooke Dec 10 '21 at 12:04
7

As explained on the xUnit GitHub page, I used

private readonly ITestOutputHelper output;

And this worked to me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stefano
  • 586
  • 7
  • 14
3

I also ran into this the first time I used xUnit after using NUnit for a long time.

Surprisingly, xUnit does not support console output directly, but does offer some other ways to achieve the same thing.

https://xunit.github.io/docs/capturing-output.html

If you aren't really invested in xUnit I would say the simplest thing to do would just use NUnit or MSTest because both support simple console.writeline.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
on3al
  • 2,090
  • 4
  • 20
  • 19
2

For the ITestOutputHelper implementation only, the following works:

As reported by Joe.wang, you must put the {add key="xunit.diagnosticMessages" value="true"} in the App.config of the unit test project.

As reported by stefano, you must add a local member of type ITestOutputHelper to the test class. And you must add it to the parameters of a constructor and assign the injected object to the local member.

You should rebuild after instrumenting with an output method.

You must have a failing test.

Your call to the output method should precede the/a failing test.

This works per test! Not for a test suite.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stato Machino
  • 1,120
  • 3
  • 13
  • 22
1

Got it to work with no configuration. Using .NET Core and VSCode, using Dependency Injection pattern, as described on XUnit.Net:

using Xunit;
using Xunit.Abstractions;

public class MyTestClass
{
    private readonly ITestOutputHelper output;

    public MyTestClass(ITestOutputHelper output)
    {
        this.output = output;
    }

    [Fact]
    public void MyTest()
    {
        var temp = "my class!";
        output.WriteLine("This is output from {0}", temp);
    }
}

And then run this command:

dotnet test --logger "console;verbosity=detailed"
umbyersw
  • 585
  • 5
  • 12
0

Json configuration

As it seems that the above xml answer was not updated for a while, is obsolete and Matt answer in a comment is not the appropriate way to answer. Here is the minimal JSON configuration needed to display messages in output.

{   
  "$schema":"https://xunit.github.io/schema/current/xunit.runner.schema.json",   
  "diagnosticMessages": true
}
Fab
  • 14,327
  • 5
  • 49
  • 68
-6

Just use Console.Write() and Console.WriteLine().

ReSharper hooks into this and you'll see the output in the test results.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131