As developers, we always write code targeted to achieve a certain feature/functionality. The piece of code is written down with best efforts to be bug free and to work exactly as expected. Unfortunately we human beings are prone to make errors & mistakes, as a result bugs do creep in. Hence it is always advisable to test out individual units of code, in order to be sure enough that your implementation actually works. With this we are introduced to the term “Unit Testing”. We can define unit testing in this manner:
It is the methodology of testing out individual units of behaviour. An individual unit of behaviour is the smallest possible unit of behaviour that can be individually tested in isolation.
Fortunately, Microsoft provides a Unit Test Framework and it is integrated with Visual Studio also. So a developer can make use the framework and start writing down unit tests, rather than worrying about setting up a framework. The framework resides inside “Microsoft.VisualStudio.QualityTools.UnitTestFramework” assembly. One thing to observer here, is that the Unit Test framework is not a part of the .NET framework. Lets create a few unit test in order to get a better understanding, I will be using Visual Studio 2013 for the demonstration.
Lets say we have class library project “UserManagement” having a “Validate” class, with a method named as “CheckStrength()” responsible for calculating the strength of the user entered password. Now we want to create unit tests targeted towards “CheckStrength()” method, in order to validate it.
Now in order to add unit tests, we need to follow these steps :
- We need to add an “Unit Test Project” to the solution which will contain all of the unit test targeted to all methods residing inside the class project
- Lets say the name of the unit test project is “UserManagementTest” as it is a general convention to suffix the word “Test”, for better clarity. Hence upon addition the solution will look like this.
- By default visual studio add a class file “Class1.cs” to the newly added project. We don’t this, as we will learn by creating our own, so we can delete this file. Also we can see that visual studio has added a reference to the “Microsoft.VisualStudio.QualityTools.UnitTestFramework” assembly for us as well.
- As we are going to write units test for the methods of “UserManagement” class library, so we have to add a reference to this project in our test project.
- After that, lets add a class to “UserManagementTest”, named as “ValidateTest” which will contain all unit tests for the methods present inside the “Validate.cs” class.
- In order to declare a class, as test class and reveal the test methods present in it, we need to mark the class with “TestClass” attribute. “TestClass” attribute is an alias for “TestClassAttribute” attribute. Similarly the test methods needs to be marked with “TestMethod” an alias for the “TestMethodAttribute” attribute. Additionally test method’s must be public, non-static method, should neither return any value nor accept any parameters. Hence the test method for “CheckStrength()” method will look like this :
- The AAA (Arrange, Act, Assert) pattern is a common way of writing unit tests for a method under test. Each test method should be grouped into these functional sections, separated by blank lines. The Arrange section contains all necessary inputs that are needed for the method under test. Act section invokes the method under test along with the required set of parameters. Assert section verifies that the action of the method under test behaves as expected. By following this pattern, we initialize the a password string and the expected score in the arrange section. In act section, we make a call to the “CheckStrength()” along with the initialized password string and in assert section we compare the expected and actual result returned by the method call. The Assert class contains a set of static overloaded methods that evaluate a Boolean condition. A truth value is returned if assertion passes. On failure an “AssertFailedException” exception is raised. This exception is handled internally by the unit test engine to indicate an assert failure, so there’s no need of user handling of exception.
- With this we are done with the unit test method. Now we are ready to run the test method, which corresponds to a test case. In order to run a test method, we need to right click on it and click on “Run Tests”.
- After tests are run “Test Explorer” window will show the test results along with a summary of test.
With this, we have successfully unit tested our method.