Getting The Most Out Of NUnit’s SetUp And TearDown
A few years ago, when I started doing Test Driven Development, the defacto .NET standard was NUnit. Ever since I’m using the NUnit framework to do my testing.
Even though there are many other frameworks (like MbUnit) with lots of cool features NUnit is missing, some neat stuff can be done using NUnit as well.
In this article I’m going to show some basic and advanced usages of SetUp and TearDown possibilities of the NUnit framework.
SetUp
The SetUp is the method executed before every test, which can be used to prepare the state of the system you are going go test.
1 [SetUp]
2 public void SetUp()
3 {
4 Console.WriteLine(“\t\t\tTest SetUp”);
5 }
TearDown
The TearDown is the method executed after every test, which can be used to restore the state of the system you have tested.
1 [TearDown]
2 public void TearDown()
3 {
4 Console.WriteLine(“\t\t\tTest TearDown”);
5 }
The important keywords here are “every test”.
One of the basic rules of Test Driven Development is that tests need to be fast. Only when tests run fast, they will be executed a lot so you can depend on the results to know that everything is working correctly.
Since logic inside SetUp and TearDown will be executed for every test, this can cause tests to become slow (certainly when you are doing a lot of database access, which is not the scope of this article).
NUnit has a solution for this, and supports TestFixture SetUps and TearDowns and even Namespace / Assembly SetUps and TearDowns (sounds nice hu?).
Assembly SetUp / TearDown
In some cases it might be handy to have some SetUp and TearDown code which will be executed once for each assembly containing your precious tests.
To enable this feature, you just add a TestClass to the root of your assembly (root namespace), and mark it with the correct attributes.
1 namespace DotNet.NUnit
2 {
3 [SetUpFixture]
4 public class AssemblyFixture
5 {
6 public AssemblyFixture() { }
7
8 [SetUp]
9 public void AssemblySetUp()
10 {
11 Console.WriteLine(“Assembly SetUp”);
12 }
13
14 [TearDown]
15 public void AssemblyTearDown()
16 {
17 Console.WriteLine(“Assembly TearDown”);
18 }
19 }
20 }
The following requirements need to be fulfilled to have it working:
- The class must be in the root namespace (root of the assembly)
- The class must be marked with the [SetUpFixture] attribute
- A public default contructor needs to be available to make sure NUnit can make an instance
- The AssemblySetUp method must be marked with the [SetUp] attribute
- The AssemblyTearDown method must be marked with the [TearDown] attribute
Namespace SetUp / TearDown
The same priciple as with the Assembly SetUp and TearDown can be applied for each Namespace inside a TestAssembly. Just add the same class to a namespace root, and it will be executed once for that namespace.
1 namespace DotNet.NUnit.Namespace1
2 {
3 [SetUpFixture]
4 public class NamespaceFixture
5 {
6 public NamespaceFixture() { }
7
8 [SetUp]
9 public void NamespaceSetUp()
10 {
11 Console.WriteLine(“\tNamespace 1 SetUp”);
12 }
13
14 [TearDown]
15 public void NamespaceTearDown()
16 {
17 Console.WriteLine(“\tNamespace 1 TearDown”);
18 }
19 }
20 }
The following requirements need to be fulfilled to have it working:
- The class must be in the root of the namespace
- The class must be marked with the [SetUpFixture] attribute
- A public default contructor needs to be available to make sure NUnit can make an instance
- The NamespaceSetUp method must be marked with the [SetUp] attribute
- The NamespaceTearDown method must be marked with the [TearDown] attribute
Fixture SetUp / TearDown
For every test fixture (test class), a one time SetUp and TearDown can be added as well. Those will be executed once before and after all tests in the same class.
1 namespace DotNet.NUnit.Namespace1
2 {
3 [TestFixture]
4 public class ClassFixture
5 {
6 [TestFixtureSetUp]
7 public void ClassSetUp()
8 {
9 Console.WriteLine(“\t\tClass 1 SetUp”);
10 }
11
12 [TestFixtureTearDown]
13 public void ClassTearDown()
14 {
15 Console.WriteLine(“\t\tClass 1 TearDown”);
16 }
17 }
18 }
The following requirements need to be fulfilled to have it working:
- The class must be marked with the [TestFixture] attribute
- The ClassSetUp must be marked with the [TestFixtureSetUp] attribute
- The ClassTearDown must be marked with the [TestFixtureTearDown] attribute
Test SetUp / TearDown
The default SetUp and TearDown as described above will be executed before and after every test.
1 [SetUp]
2 public void SetUp()
3 {
4 Console.WriteLine(“\t\t\tTest SetUp”);
5 }
6
7 [TearDown]
8 public void TearDown()
9 {
10 Console.WriteLine(“\t\t\tTest TearDown”);
11 }
The full structure of the project looks like this:

A final test class, with all features included as described above, can look like this:
1 namespace DotNet.NUnit.Namespace1
2 {
3 [TestFixture]
4 public class ClassFixture
5 {
6 [TestFixtureSetUp]
7 public void ClassSetUp()
8 {
9 Console.WriteLine(“\t\tClass 1 SetUp”);
10 }
11
12 [SetUp]
13 public void SetUp()
14 {
15 Console.WriteLine(“\t\t\tTest SetUp”);
16 }
17
18 [TestFixtureTearDown]
19 public void ClassTearDown()
20 {
21 Console.WriteLine(“\t\tClass 1 TearDown”);
22 }
23
24 [TearDown]
25 public void TearDown()
26 {
27 Console.WriteLine(“\t\t\tTest TearDown”);
28 }
29
30 [Test]
31 public void Test1()
32 {
33 Console.WriteLine(“\t\t\t\tClass 1 - Test 1″);
34 }
35
36 [Test]
37 public void Test2()
38 {
39 Console.WriteLine(“\t\t\t\tClass 1 - Test 2″);
40 }
41 }
42 }
When executing all tests in the assembly using the NUnit Gui, we can see the following output printed out on the console:


The following conclusions can be made based on the result above:
- The AssemblySetUp is executed once at the beginning of the full assembly
- The AssemblyTearDown is executed once at the end of the full assembly
- The NamespaceSetUp is executed once at the beginning of the full namespace
- The NamespaceTearDown is executed once at the end of the full namespace
- The ClassSetUp is executed once at the beginning of the full class
- The ClassTearDown is executed once at the end of the full class
- The SetUp is executed at the beginning of each test
- The TearDown is executed at the end of each test
This was an introduction to some more advanced SetUp and TearDown possibilities of NUnit. Using it in a real project depends on your specific needs, but the above can be used as a good starting point if you’ll ever need it.