Test Initialize and Test Setup

Fundamental of unit testing: understand TestInitializer and TestCleanup attribute

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 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 by a TestInitialize attribute and after 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 our 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.

  1. namespace TestProjectLibrary  
  2. {  
  3. public class testClass  
  4. {  
  5. public string testFun()  
  6. {  
  7. return "done";  
  8. }  
  9. }  

Fine, here is the unit test class.

  1. using System;  
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  3. using TestProjectLibrary;  
  4.   
  5. namespace UnitTest  
  6. {  
  7.   
  8. [TestClass]  
  9. public class UnitTest  
  10. {  
  11. testClass objt;  
  12. string rvalue;  
  13.   
  14. [TestCleanup]  
  15. public void testClean()  
  16. {  
  17. objt = null;  
  18. rvalue = String.Empty;  
  19. }  
  20.   
  21. [TestInitialize]  
  22. public void testInit()  
  23. {  
  24. objt = new testClass();  
  25. rvalue = "done";  
  26. }  
  27.   
  28. [TestMethod]  
  29. public void TestMethod1()  
  30. {  
  31. Assert.AreEqual(rvalue, objt.testFun());  
  32. }  
  33.   
  34. [TestMethod]  
  35. public void TestMethod2()  
  36. {  
  37. Assert.AreEqual(rvalue, objt.testFun());  
  38. }  
  39. }  

 

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 are seeing that both tests passed.


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 testiing.