xunit doesn't write message to Output pane

In VS 2015 Community I have a sample ASP.NET 5 (vNext) project and a project with unit-tests (xUnit.net). Version of DNX is 1.0.0-beta5. My goal is to add messages during test run to 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 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 selected test tun info, like here : (Only "Test passed... Elapsed time ...")

What should I do to make this ITestOutputHelper work?

Answers


Please try this solution. It works for me .(VS2017 xunit 2.2.0.3545).

Try adding below configuration in the App.Config file. (I don't know why. It just needed. If there doesn't exit, 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 

Hope it is helpful. Please let me know if it works. Thanks.


As explained in xunit github I used

private readonly ITestOutputHelper output;

and this worked to me


I also ran into this the first time I used XUnit after using Nunit for a long time. Suprisingly 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


For the ITestOutputHelper implementation only, the following works:

As reported by @joe, 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.


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

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


Need Your Help

SysUtils´╣Ľ ApplicationContext is null in ApplicationStatus (WebView)

java android webview

I can see something strange at WebView based app start. It doesn't affect the app at all but I'm curious why the error appears. Nothing crashes, everything seems to be ok, but the error...

Learning Ant path style

java ant path conventions

Where can I find resources to learn Ant path style conventions? I've gone to the Ant site itself, but couldn't find any information on path styles.