Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xUnit.net does not capture console output

Tags:

xunit.net

I just started testing xUnit.net, but it doesn't seem to capture any output (Console, Debug, Trace), as I would have expected.

Is that possible? I am using a sample .NET 4.0 class-library with xUnit.net 1.8.

like image 476
kfuglsang Avatar asked Aug 21 '11 14:08

kfuglsang


People also ask

Does xUnit work with .NET framework?

xUnit.Net is an open source unit testing tool for the . Net Framework that provides an easy way to work with data driven unit tests.

Does xUnit work with .net 5?

Sometimes, you want to write tests and ensure they run against several target application platforms. The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and .

Where is xUnit console exe?

In the tools folder of the console runner package folder, you will find xunit. console.exe .


1 Answers

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!

like image 52
Josh Gallagher Avatar answered Oct 07 '22 03:10

Josh Gallagher