Test Initialize and Test Setup

Testing Environment Setup

This is the “fundamental of unit testing” article series. Here we are talking about unit testing and the fundamental concept of it. In our previous articles, we covered many important topics, you can read them here.

This article explains the uses of two very important attributes called TestInitialize and TestCleanup. Both attributes are useful at the time of unit test setup. Let's understand the theory behind them then we will implement them practically.

Since they are attributes we can set them with any function. Here is their purpose.

TestInitialize

This attribute is needed when we want to run a function before execution of a test. For example, we want to run the same test 5 times and want to set some property value before running each time. In this scenario, we can define one function and decorate the function with a TestInitialize attribute.

TestCleanup

This attribute is like the opposite of TestInitialize, each time the function is decorated with this attribute it will execute when test execution finishes. So, after the execution of each and every test if we need to perform some cleanup operation then we can write code within this function.

Ok, so let me discuss one real scenario using both of them. For example, we need to perform a unit test to a function where the function will get data from a file and we need to test the function 5 times with various input values, and depending on the input value the file will be created. Oh, it is a little complex situation.

So, we can divide the entire operation in this way, at first we will create a file using a function that is decorated with a TestInitialize attribute and after the performance of a test we will delete the file in the function that is decorated with a TestCleanup attribute.

Fine, so let's implement one small example.

Here is the class that we want to test using a unit test application. If you are very new to a unit test, I will suggest you to go through our previous articles.

namespace TestProjectLibrary   
{   
    public class testClass   
    {   
        public string testFun()   
        {   
            return "done";   
        }   
    }   
} 

Fine, here is the unit test class.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestProjectLibrary;

namespace UnitTest
{
    [TestClass]
    public class UnitTest
    {
        testClass objt;
        string rvalue;

        [TestCleanup]
        public void testClean()
        {
            objt = null;
            rvalue = String.Empty;
        }

        [TestInitialize]
        public void testInit()
        {
            objt = new testClass();
            rvalue = "done";
        }

        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual(rvalue, objt.testFun());
        }

        [TestMethod]
        public void TestMethod2()
        {
            Assert.AreEqual(rvalue, objt.testFun());
        }
    }
}

If you look closely at the testInit() method then you will find that we are creating a new object of testClass() and setting “rval” that will store a return value and within the testclear() function we are clearing both.

Here is our output and we see that both tests passed.

Output

Conclusion

I hope you have understood the importance of both attributes. Both are very essential for unit test setup. In our next article, we will understand a few more features of unit testing.


Similar Articles