I have been doing some research on test driven development and find it pretty cool.
One of the things I came across was that when you write your tests, there is an orde
For each class that you have tests in, a test fixture, you can specify 4 special methods. The names of the methods are not really important, but you need to tag the methods with one of the following four attributes in order to identify them.
Convention dictates that you call the methods the same as the attributes though, but as I said, the attributes is the important bit.
Note that the attributes I describe here are the ones found in NUnit, but similar attributes (if not the same) are in use in most unit test frameworks.
The attributes are:
The first two has to do with the class as a whole. The method tagged with the TestFixtureSetUp attribute is run once, before the first test in the class.
After all the tests in the class has been executed, the method tagged with the TestFixtureTearDown attribute is executed, once.
You can use these two to prepare common data structures that are the same for all the tests, and aren't modified by any tests (this is important).
The last two, SetUp and TearDown, are used to tag two methods that will be run before, and after each individual test.
The method tagged with SetUp is called before each test, and the method tagged with TearDown is called after each test. You can use these to prepare common data structures, that though they are the same for each test, they will be changed by some or all of the tests, so it's best to prepare a new fresh copy for each test.
Laying out the execution of these methods as pseudo-code gives us this order:
execute TestFixtureSetUp, if present
for each test do
execute SetUp, if present
execute actual test
execute TearDown, if present
execute TestFixtureTearDown, if present
The usage of these attributes are entirely optional. You do not need to have a SetUp in order to have a TearDown or vice versa. They're just points you might want to execute code at.