Arnis Lapsa
Arnis Lapsa

Reputation: 47647

NUnit SetUpFixture attribute equivalent in xUnit?

In nUnit, SetUpFixture allowed me to run some code before any tests. Is there anything like that when using xUnit?


From nUnit documentation:

This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace.

Upvotes: 6

Views: 7050

Answers (2)

Mark Rushakoff
Mark Rushakoff

Reputation: 258408

xUnit's comparison table shows that where you would use [TestFixtureSetUp] in NUnit, you make your test fixture class implement IUseFixture<T>.


If [TestFixtureSetUp] isn't the attribute you're looking for, then the header at the beginning of the compatibility table indicates that there is no equivalent:

Note: any testing framework attributes that are not in this list have no corresponding attribute in xUnit.net.

Upvotes: 5

cyclst
cyclst

Reputation: 1

I know this is an old topic, but for anyone that comes looking, you can create a base fixture class which implements IClassFixture. Your test fixtures can all inherit from this base class.

You could inject singleton classes to this base class if you need stuff to run assembly wide.

internal abstract class TestFixtureBase : IClassFixture<TestApplicationFactory>, IAsyncLifetime
{
    protected TestFixtureBase(TestApplicationFactory factory)
    {
    //...
    }

    public async Task InitializeAsync()
    {
    }

    public Task DisposeAsync()
    {
        //...
    }
}

Upvotes: 0

Related Questions