xUnit framework: Equivalent of [TestFixtureSetUp] of NUnit in XUnit?

user819492 picture user819492 · Jun 28, 2011 · Viewed 26.4k times · Source

What is xUnit's equivalent of NUnit's [TestFixtureSetUp]?

We have explored and found that IUseFixture<T> is the equivalent of [TestFixtureSetUp], but it's not working as expected.

As we have explored (in case of NUnit), we found that [TestFixtureSetUp] marked methods for code to be executed only once before all test in the fixture have been run. In xUnit, the equivalent of [TestFixtureSetUp] is IUseFixture<T> as we have explored, but during testing, we found that the SetFixture method of IUseFixture is being called before every test (not only once for all methods).

Please let us know how can we achieve the above in xUnit. Also correct us if we are misunderstanding something. Thanks.

Answer

Laepdjek picture Laepdjek · Jun 29, 2011

There is no direct equivalent of [TestFixtureSetUp] in XUnit, but you can achieve similar functionality. This page lays out the translation between NUnit and XUnit (as well as a couple other C#/.NET test frameworks). However, XUnit largely got rid of setups/teardowns (this article explains why that decision was made). Instead, you need the test suite to implement an interface called IUseFixture<T> which can initialize some data for the fixture.

You might also want to read this overview of XUnit, written from the perspective somebody coming from an NUnit/MbUnit background.