This is the "Fundamentals of unit testing" article series. In our previous article we had an introduction to unit testing. We have learned why unit testing is very important and defined steps of Test Driven Development. You can read it here.
Fundamentals of Unit Testing: Getting Started With Unit Testing
Ok, as promised, in this article we will implement a simple unit testing application and will see how unit testing should be performed in the Visual Studio environment. So, let's go step-by-step.
Step 1: Create a class library application
Here we will create a class library application, the class library application is purposefully chosen because as per our discussion in the previous article, a unit test is a best fit for applications where there is no user interface. So here is the class library.
Step 2: Define simple class and function
Here we will define a simple class and function to be tested by a unit test method. For the sake of simplicity, we have implemented a very simple string handling function. Have a look at the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestProjectLibrary
{
public class StringHandling
{
public string concat(string name, string surname)
{
return name + surname;
}
}
}
So, the purpose of the concat() function is to return a string after adding two string parameters that it will take as input.
Step 3: Add Unit Test project in solution
Here we will add a unit test project to the solution. As we have discussed, we will use a unit test template of Visual Studio. So, add a Unit Test Project to the solution.
After adding the project to the solution, the solution will look something similar to this.
Step 4: Add reference to UnitTest application
In this step we will provide a reference of the Class Library project in the Unit Test project. Here is the reference screen.
Step 5: Write test case to perform testing
Now, go to the UnitTest project and open the UnitTest1.cs file and modify it with following code.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestProjectLibrary;
namespace UnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
StringHandling ObjString = new StringHandling();
String rvalue = ObjString.concat("sourav", "kayal");
Assert.AreEqual("sourav", rvalue);
}
}
}
So the code is very simple and self-explanatory, the class is defined as a unit test class and the method is decorated as a TestMethod. Then within the method, we are creating an object of the class, to which we will test and call the function.
The function will return one string, we are expecting it will return the concatenation value of both arguments. Then we are using the AreEqual() function of the Assert class that will match the return value with the expected value.
The first argument is the expected value and the second parameter is the returned value. Now we need to run the test case.
The point to make is that we cannot run the unit test application like normal application. To run it, go to:
Test/All Tests and click on that. We will see the following output window.
And we will see that the test did not pass because the argument we are passing is “Sourav” and “kayal” and the return value will be “souravkayal” but we are expecting “Sourav” from the function. So it fails.
Now if we change the expected value in the AreEqual() function as in the following.
We will see that the test has passed.
Cheers! We have learned to implement a simple unit testing application. In a future article we will explore more of unit testing.