/ Cloud

Getting Console.Write to work in dotnet test

Today I was debugging a test at commandline with dotnet core and was suprised to see that when using xunit Console.Write messages were not bubbled up to the command line.

After a little research i discoverd that xunit wants you to use ITestOutputHelper

So to do that in file that contains my test I add the following dependencies

using Xunit.Abstractions;

I then add a private variable:

private readonly ITestOutputHelper output;

The in the constuctor of the test class I add:

public Testing(ITestOutputHelper output){
            this.output = output;

Finally in the test I add a call to the output:

 output.WriteLine("The third Element is {0}", thirdelement);

You will the find when you run the test from the commandline using dotnet test the output is displayed when the test fails if the test passes output is not reported.

Martin Beeby

Martin Beeby

Martin works for Oracle as a Developer Evangelist. He’s been a developer since the late 90s and loves figuring out problems and experimenting with code.

Read More