Farfi
Farfi

Reputation: 483

xUnit doesn't write message to the output pane

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?

Upvotes: 41

Views: 20539

Answers (7)

Fab
Fab

Reputation: 14813

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
}

Upvotes: 0

umbyersw
umbyersw

Reputation: 615

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"

Upvotes: 4

Stato Machino
Stato Machino

Reputation: 1190

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.

Upvotes: 2

stefano
stefano

Reputation: 606

As explained on the xUnit GitHub page, I used

private readonly ITestOutputHelper output;

And this worked to me.

Upvotes: 7

on3al
on3al

Reputation: 2140

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.

Upvotes: 3

Joe.wang
Joe.wang

Reputation: 11791

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

Upvotes: 8

Steffen Cole Blake
Steffen Cole Blake

Reputation: 654

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

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

Upvotes: -6

Related Questions