TestFixtureSetUp and TestFixtureTearDown in Unit Testing Using NUnit: Part 4

This article is the fourth in the series about Unit Testing using NUnit. Read the first three parts of this series below:

  1. How to do Unit Test using NUnit : Part 1

  2. Test SetUp and TearDown in Unit Testing using NUnit : Part 2

  3. Ignore Test in Unit Testing using NUnit: Part 3

In Part 2 , we discussed TestSetUp and TearDown. Notice that TestSetUP and TearDown are related to tests. They are both executed each time a test is executed. So if there are 5 tests in a Test Fixture then TestSetUp and TearDown will be executed 5 times. Sometimes this is good and is needed for certain requirements. However doing heavy tasks, like cleaning up an entire database and so on each time for a test may not be a good idea. You may want to perform a certain task for an entire test fixture rather than for each and individual test.

You can execute certain code before execution of all the tests in a test fixture using attribute codes with [TestFixtureSetUp]. This is used to create a setup for all tests at one instant.

So you can create a TestFixtureSetup or setup for an entire test as follows:


Basically we use TestFixtureSetUp to set up for all tests. On the other hand you can clean up all tests once using TestFixtureTearDown.


TestFixtureSetUp and TestFixtureTearDown are used to set up a test and clean up a test. I hope you find this article useful. Thanks for reading.