2

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
Community
  • 1
  • 1
ccoutinho
  • 3,308
  • 5
  • 39
  • 47
  • How do you create an instance of TestsFixture? – Valentin Dec 10 '15 at 14:27
  • @Valentin That is not done by me, but within IClassFixture. I added some more information to the question. Nonetheless, if I remove the printing the test is executed successfully, so I don't believe that to be the problem – ccoutinho Dec 10 '15 at 15:26
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Dec 11 '15 at 09:55

1 Answers1

2

The documentation says that you can add a parameter of type ITestOutputHelper to the constructor of the test class. I don't see anything that says you can add it as a parameter to the constructor of a test fixture class...

It wouldn't make sense for this output to go via ITestOutputHelper because the whole point of that mechanism is to allow the output to be associated with a specific test. Your setup/teardown is global, not per-test.

You'll need to find another way to output those diagnostics.

Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • Makes sense. Thank you so much for your explanation! Do you know about such a way? – ccoutinho Dec 11 '15 at 14:35
  • @rsy: not offhand, no - and I guess it depends on where you want the output to appear. Are you sure that `Console.WriteLine` won't work? I know that's no longer written to the test results, but maybe it's still written to the output? If not, try using a `TraceListener` (from `System.Diagnostics`) and doing `Trace.Write...`. – Gary McGill Dec 11 '15 at 14:40
  • It is not outputted to the output. I'll read about the TraceListener. Thank you again! – ccoutinho Dec 11 '15 at 15:34