As I mentioned in my previous post that in this post we will be discussing about test structure that we will are writing.
Here I will be explaining in brief different types of attributes that can be used while writing our tests. Till now we have just used one attribute i.e. [TestMethod] which contains the test to be executed.
There are other important attributes that can be used during tests to make tests well organized.
Few of the attributes are as given below:
- [ClassInitialize]
- [ClassCleanup]
- [TestInitialize]
- [TestCleanup]
- [AssemblyInitialize]
- [TestMethod]
- [TestClass]
[ClassInitialize] attribute can be used with a method that you want to execute before intialization of a class. So, let's suppose there are five tests in a test class then the method having [ClassInitialize] attribute will run only once for this class before running all the tests.
[ClassCleanup] attribute can be used with a method that you want to execute after all the tests in the class are run.
If you want a code to execute before execution of each test than [TestInitialize] is the right attribute for you. Methods with this attribute will run before execution of a test.
[TestCleanup] attribute can be used with a method that you want to execute after execution of each test. So, if you have something that you want to run after each test than just put that code in a method with this attribute.
[AssemblyInitialize] attribute can be used with a method that you want to execute before all tests and classes i.e. it runs before running any test in a test assembly.
[TestMethod] is the attribute which we were using till now. Methods with this attribute is the unit test that we want to execute for a particular scenario. So, we put all the neccessary steps that we want to execute in this method.
All of above attributes have there existence if there is [TestClass] attribute used with the class. If this attribute is not used than nothing will be executed. It can be seen that many times people just write everything but forget to include this attribute with the class. While executing tests nothing gets executed and spends lots of time in finding what actually went wrong. So, don't forget to include this attribute.
Summary:
In short, if there is on test class TESTCLASS_A with five test methods and TESTCLASS_B with five test methods. Also, there is a TESTCLASS_C with [AssemblyInitialize] method and this class is inherited by TESTCLASS_A and TESTCLASS_B.
Now, if you execute all tests, AssemblyInitialize method will be called before running any other method and will called just once. After that within each class methods will be called in below order:
- ClassInitialize
- TestInitialize
- TestMethod..1
- TestCleanup
- TestMethod..2.
- TestCleanup and so on till TestMethod..n
- ClassCleanup
In my later posts we will be using these attributes in our examples. In my next post, we will be discussing about logging and I will explain you how we can configure log4net so that we can log to console or files while our tests executes.
Thanks for reading this post and see you in the next post. cya!
Comments
Post a Comment