xUnit.net does not capture console output

The situation has changed a little with xUnit.net 2. I know the question is about an earlier version, but as people will land here having performed the upgrade, I thought it was worth pointing this out.

In order to see some kind of output in the test output in version 2 you will need to take a dependency in your test class (via a constructor argument) on an instance of Xunit.Abstractions.ITestOutputHelper, then use the WriteLine method on this interface. E.g.:

public class MyTestSpec
{
  private readonly ITestOutputHelper _testOutputHelper;

  public MyTestSpec(ITestOutputHelper testOutputHelper)
  {
    _testOutputHelper = testOutputHelper;
  }

  [Fact]
  public void MyFact()
  {
    _testOutputHelper.WriteLine("Hello world");
  }
}

You could choose to hook up your logging framework to this interface, perhaps by injecting an ILog implementation that forwarded all calls to ITestOutpuHelper.

I acknowledge that you won't want to do this by default, but for diagnostic purposes from time to time it can be quite useful. This is especially true where your tests only fail on some cloud based build & test server!


This can help if your Console.Write is embedded deep down some class hierarchy that you don't want to refactor:

    public MyTestClass(ITestOutputHelper output)
    {
        var converter = new Converter(output);
        Console.SetOut(converter);
    }

    private class Converter : TextWriter
    {
        ITestOutputHelper _output;
        public Converter(ITestOutputHelper output)
        {
            _output = output;
        }
        public override Encoding Encoding
        {
            get { return Encoding.Whatever; }
        }
        public override void WriteLine(string message)
        {
            _output.WriteLine(message);
        }
        public override void WriteLine(string format, params object[] args)
        {
            _output.WriteLine(format, args);
        }

        public override void Write(char value)
        {
            throw new NotSupportedException("This text writer only supports WriteLine(string) and WriteLine(string, params object[]).");
        }
    }

I used Console.SetOut to output Console.Writes to .NET Test Log (in Visual Studio Code).

using System;
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace UnitTest
{
    public class TestClass
    {
        private ITestOutputHelper output;
        public TestClass(ITestOutputHelper output)
        {
            this.output = output;
        }

        public class ConsoleWriter : StringWriter
        {
            private ITestOutputHelper output;
            public ConsoleWriter(ITestOutputHelper output)
            {
                this.output = output;
            }

            public override void WriteLine(string m)
            {
                output.WriteLine(m);
            }
        }

        [Fact]
        public void TestName()
        {
            Console.SetOut(new ConsoleWriter(output));
            Assert.True(ToBeTested.Foo());
        }



        public class ToBeTested
        {
            public static bool Foo()
            {
                Console.WriteLine("Foo uses Console.WriteLine!!!");
                return true;
            }
        }

    }

}

But it is easier to just run the test via console

dotnet test 

There the output will be shown without any modifications of test class.

It is different since the .NET Test Log-window uses the TRX-format (Visual Studio Test Results File), see

dotnet test -h | grep logger

For more information about TRX.


There is a solution as found here: https://xunit.codeplex.com/discussions/211566

Simply add this to your constructor or method where you want debugging output:

Debug.Listeners.Add(new DefaultTraceListener());