views:

128

answers:

3

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.

+2  A: 

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.

Mark Rushakoff
I guess that one is per fixture. I need one that is per whole assembly.
Arnis L.
Workaround would do. And I read that xUnit is quite extensible. Just thinking - can't be that no one have faced this problem.
Arnis L.
+1d - xUnit Test Patterns (and extensive use of xUnit.net) says you use `IUseFixture<TFixture>` and let the impl of the fixture class set up the appropriate prerequisites, treating the fact that you only want to do it once per assembly as an optimisation within the fixture (maybe cache stuff in a static or whatever is appropriate). If you want to do very wacky stuff, look at a custom runner-class a la the SubSpec sample (download full source - iits quite short)
Ruben Bartelink
+1  A: 

This was covered on the xunit forum in this discussion. (And most recently in this question)

Ruben Bartelink
A: 

Actually, the setup and tear-down is mentioned in the book "The Art of Unit Testing", which says that the attribute is called "[SetUp]" and not "[SetUpFixture]", which is not listed in the comparison table anyway.

So... for the "[SetUp]" attribute, the xUnit.net equivalent is just to use a constructor. The corresponding comment says:

'We believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement. See Note 2. '

Further down, Note 2 says:

Note 2: The xUnit.net team feels that per-test setup and teardown creates difficult-to-follow and debug testing code, often causing unnecessary code to run before every single test is run.

shahedc
Arnis L.