Search code examples
c#xunit

How can I color code output from ITestOutputHelper.WriteLine


To ease debugging I'm writing out some information when a unit test fails. This comes out as white text and is hard to read. I'd like to color code this, is it possible?

https://xunit.net/docs/capturing-output

    public TestClass(ITestOutputHelper output)
    {
        _log = output;
    }
    [Fact]
    public void RequiredAttributeTest()
    {
        ....setup code
        foreach(var error in results)
            {                        
                _log.WriteLine(error.ErrorMessage);
             }
         }
         Assert.Equal(false, isValid);
     }

Solution

  • I didn't test it, but after reading a few of xUnit's sources, I'd say it is not possible.

    According to this issue and this piece of code, ANSI color support should work when running your xUnit test suite on Linux.

    But ConsoleHelper.SetForegroundColor is internal and will be set by DiagnosticMessageSink - an object you can retrieve via xUnit's output functionality (see here), but the console color cannot be set publicly (see here).

    Maybe you can ask Brad Wilson, the maintainer of xUnit, if he has further ideas of plans to support this feature.