Test SetUp and TearDown in Unit Testing Using NUnit : Part 2

Read Part 1 of this series here: How to do Unit Test using NUnit : Part 1

The last article explained how to start with Unit Testing using NUnit. In this article I will discuss the following two topics:

  1. Test Setup
  2. Test Teardown

You need Test Setup and Test Teardown to remove any dependency between tests. Assume a scenario that you want to:

  • Create an instance of a specific object before execution of any test
  • Delete a specific file from the file system before execution of any test
  • Insert some test data or create some test data before execution of any test
  • and so on

In the above stated scenario you may want to create a Test Setup. A Test Setup is a piece of code executed before execution of any test.

UnitTesting1.jpg

Another use case could be that you want to perform a specific task after execution of each test. So once a test is executed a certain task should be done and we call that a Test Teardown. There could be a scenario that you want to:

  1. Destruct an instance after execution of any test
  2. Remove test data after execution of any test
  3. Delete a file from the file system after execution of any test
  4. and so on.

In the above scenario you may want to create a Test Teardown. A Test Teardown is a piece of code executed after execution of a test.

UnitTesting2.jpg

In NUnit you can create a Test Setup and a Test Teardown by using [Setup] and [TearDown] attributes on a function.

So a Test Setup can be created as in the following:

UnitTesting3.jpg

And you can create a Test Teardown as in the following:

UnitTesting4.jpg

If there are 5 tests in your test class then these two functions will be executed 5 times. Now let us put our explanation into a concrete example. Assume that you are writing a Unit Test for a Product class. The Product class is defined as in the following:

namespace MyAppToTest

{

   public class Product

    {

       double productPrice;

      

       public double ProductPrice

       {

           get

           {

               return productPrice;

           }

 

           set

           {              

                    productPrice = value;

           }

       }

    }

}

A Unit Test is written to test the validity of the product price as in the following:

[Test]

        public void IsValidProductPrice()

        {

           

            p.ProductPrice = 100;

            if (p.ProductPrice > 0)

            {

                result = true;

            }

 

            Assert.IsTrue(result, "Product Price is valid");

        }

You can write a Test SetUp and TearDown as in the following:

Product p;

        bool result;

        [SetUp]

        public void TestSetup()

        {

            p = new Product();

            result = false;

           

        }

 

        [TearDown]

        public void TestTearDown()

        {

            p = null;

            result = false;

        }

The preceding two functions will be executed each time before execution of each test and after execution of each test. In writing a Unit Test, Test SetUp and Test TearDown are very handy and useful.

I hope you find this article useful. Thanks for reading.

Further Readings



Up Next
    Ebook Download
    View all
    Learn
    View all