In xUnit I need to run some code once, before any test is executed, and also some code after all tests are done. Although this thread explains how to do it quite well, I want to do some printing inside the constructor and destructor, like shown in the code below, and that's the tricky part. Since Console.Writeline
won't work, I looked for a workaround, which I found in this link.
public class TestsFixture : IDisposable
{
protected readonly ITestOutputHelper _output;
public TestsFixture(ITestOutputHelper output)
{
_output = output;
// Do "global" initialization here; Only called once.
_output.WriteLine("global init");
}
public void Dispose()
{
// Do "global" teardown here; Only called once.
_output.WriteLine("global teardown");
}
}
public class HandlerTests : IClassFixture<TestsFixture>
{
// All my tests are here
}
A brief explanation on what's going on here:
This code uses the IUseFixture interface to ensure that the global initialization/teardown functionality is only called once. For this version, you don't extend a base class from your test class but implement the IUseFixture interface where T refers to your fixture class
Everything seems fine, but when I run the tests, I get an error (below). Any idea on how to solve this problem?
Test Outcome: Failed Test Duration: 0:00:00,001 Result Message: Class fixture type 'TestsPlatform.TestsFixture' had one or more unresolved constructor arguments: ITestOutputHelper output