It is therefore essential to get the fundamentals of unit testing early in your development stages to avoid the learning curve when dealing with extensive codebases.
In this tutorial, we will introduce you into the framework of unit testing using C# and the MSTest testing library. We will build a basic application that will allow you to learn how to create the unit tests, how to run them, and how to interpret the results from a failure or success of a test.
Testing Environment
Let us start by discussing the test solution and how to configure our basic application.
To follow and replicate the application in this tutorial, ensure that you have the following:
- Installed Visual Studio
- Configured .NET and C# tools on your machine
Create an Application
Next, open the Visual Studio and create a new solution. For this tutorial, we will build a basic calculator application. You can give the application with any name that you desire.
Next, create a new project and select the project type as MSTest Test Project and give it any desirable name.
Ensure to select “Add to solution” and choose the previously created app.
You should have the main application and the unit testing project as shown in the solution explorer.
Write the Code for the Calculator Application
The next step is writing the code for our calculator application. We can do this in the CalcApp “program.cs” file.
Since we are looking for basic functionality, we can add the source code as follows:
{
public double Add(double a, double b)
{
return a + b;
}
public double Subtract(double a, double b)
{
return a - b;
}
public double Multiply(double a, double b)
{
return a * b;
}
public double Divide(double a, double b)
{
if (b == 0)
throw new DivideByZeroException();
return a / b;
}
}
As you can guess, the previous code creates a calculator app that can perform the basic arithmetic applications.
Write the Unit Tests
Once we are done, we can write the unit tests for the “Calculator” class. In this case, all we need to do is write the tests in the “UnitTest” file of the project as follows:
namespace CalcApp
{
public class Calculator
{
public double Add(double a, double b)
{
return a + b;
}
public double Subtract(double a, double b)
{
return a - b;
}
public double Multiply(double a, double b)
{
return a * b;
}
public double Divide(double a, double b)
{
if (b == 0)
throw new DivideByZeroException();
return a / b;
}
}
}
namespace CalcTest
{
[TestClass]
public class CalculatorTests
{
private Calculator calculator;
[TestInitialize]
public void Setup()
{
calculator = new Calculator();
}
[TestMethod]
public void Add_TwoPositiveNumbers_ReturnsCorrectSum()
{
var result = calculator.Add(5, 5);
Assert.AreEqual(10, result);
}
[TestMethod]
public void Subtract_TwoPositiveNumbers_ReturnsCorrectDifference()
{
var result = calculator.Subtract(10, 5);
Assert.AreEqual(5, result);
}
[TestMethod]
public void Multiply_TwoPositiveNumbers_ReturnsCorrectProduct()
{
var result = calculator.Multiply(5, 5);
Assert.AreEqual(25, result);
}
[TestMethod]
[ExpectedException(typeof(DivideByZeroException))]
public void Divide_DenominatorIsZero_ThrowsDivideByZeroException()
{
var result = calculator.Divide(5, 0);
}
[TestMethod]
public void Divide_TwoPositiveNumbers_ReturnsCorrectQuotient()
{
var result = calculator.Divide(10, 5);
Assert.AreEqual(2, result);
}
}
}
Conclusion
This is a basic tutorial that covers the fundamentals of unit testing in C# using the MSTest framework.