I recently started using XUnit. I’m still discovering what is possible using this unit test framework.
One of the things I had to discover was how to write log messages during my tests. I could fall back to using Trace.WriteLine or Console.WriteLine but that is not the best solution. XUnit allows you to capture test output using a special interface called
Now if you call the ITestOutputHelper.WriteLine method, the output is captured and available inside your test output:
One of the things I had to discover was how to write log messages during my tests. I could fall back to using Trace.WriteLine or Console.WriteLine but that is not the best solution. XUnit allows you to capture test output using a special interface called
ITestOutputHelper
. In order to use this, just add a constructor argument for this interface and store it inside a variable inside your test class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailTests : IDisposable | |
{ | |
private readonly ITestOutputHelper _testOutputHelper; | |
public MailValidationTests(ITestOutputHelper testOutputHelper) | |
{ | |
_testOutputHelper = testOutputHelper; | |
} | |
[Fact] | |
public void Mail_MustHaveASubject() | |
{ | |
} | |
} |
Now if you call the ITestOutputHelper.WriteLine method, the output is captured and available inside your test output: