In a previous post Ben Lowery asked the question? Why is the collection in your test fixture class static? Here is the code from the previous post:
public class BookmarkCollectionFixture
private static BookmarkCollection collection;
public void BeforeTest()
collection = new BookmarkCollection();
The reason that I do this is not so subtle, so let me explain. I think one of the biggest screw-ups that was made when we wrote NUnit V2.0 was to not create a new instance of the test fixture class for each contained test method. I say “we” but I think this one was my fault. I did not quite understand the reasoning in JUnit for creating a new instance of the test fixture for each test method. I look back now and see that reusing the instance for each test method allows someone to store a member variable from one test and use it another. This can introduce execution order dependencies which for this type of testing is an anti-pattern. It is much better to fully isolate each test method from the other. This requires that a new object be created for each test method.
Back to the question at hand… Since it would be difficult to change the way that NUnit works now, too many people would complain, I now make all of the member variables in test fixture classes static. It’s almost like truth in advertising. The result is that there is only one instance of this variable no matter how many test fixture objects are created. If the variable is static then someone who may not be familiar with how NUnit executes would not assume that a new one is created before each test is executed. This is the closest I can get to how JUnit works without changing the way that NUnit executes test methods.
For a great discussion of this topic see Martin Fowler’s bliki entry: JUnitNewInstance