[TUTORIAL] Write Unit Test In C# With NUnit

At university, you must have learned the concept of Unit Test in “Software Quality Testing.” In simple terms, unit testing is the code used to test the code we have written.

Photo by Chris Ried on Unsplash

Some characteristics of unit tests:

Some of you will ask: “Oh well, in the business class, do we have to call data access?”, And “our code is very sticky, how can we test each class separately.”

Some benefits of Unit Test:

Photo by Ferenc Almasi on Unsplash

Recently, the Test Driven Development (TDD) development model has been getting hot, applied a lot. This model is based on the concept: For each function, we write the Unit Test first, then write the function implementation function to unit test pass.

Unit Test in C # with NUnit

Previously, to write Unit Test in C #, we often had to create a separate test project using Microsoft’s MSTest library. MSTest supports many functions: Test data from the database, measure system performance, export report data. However, because MSTest comes with Visual Studio, it cannot be run separately, it is quite heavy, so NUnit is more popular. NUnit has its own set of runners, which can run UnitTest independently without VisualStudio, besides it also supports some features that MSTest does not have (parameter test, Assert Throw).

If you have never written Unit Test, starting with NUnit is also a good choice. First, let’s create a project console in Visual Studio. We write a class Calculator with two functions Add and Subtract:

public class Calculator
{
public int Add(int x, int y)
{
return x + y;
}
public int Subtract(int x, int y)
{
return x - y;
}
}

Now, we will write UnitTest to test these two functions. Normally, the Unit Test code will be located in a separate Test project, so we will create a new project, adding “.Test” after the name. Remember to choose the project type as Library Class.

On the new project, click Tools -> Library Package Manager -> Package Manager Console, type: “Install-Package NUnit -Version 2.6.4” to install NUnit. We create a new class named CalculatorTest. Let’s start writing:

using NUnit.Framework;
[TestFixture]
public class CalculatorTest
{
private Calculator _cal;

[SetUp]
public void Setup()
{
_cal = new Calculator();
}
[Test]
public void OnePlusOneEqualTwo()
{
Assert.AreEqual(2, _cal.Add(1, 1));
}
[Test]
public void TwoPlusTwoEqualFour()
{
Assert.AreEqual(4, _cal.Add(2, 2));
}
[Test]
public void FourPlusOneEqualFive()
{
Assert.AreEqual(5, _cal.Add(4, 1));
}
}

Explain some annotation:

To run the Unit Test, we need to install NUnit.TestAdapter, which is a runner that allows running NUnit Unit Test in Visual Studio. You can also use either of the following:

To install TestAdapter for Visual Studio, go to Tools, Extensions, and Updates, find NUnit and install it. After installing, remember to Restart Visual Studio.

Click Test -> Run -> All Tests to start running. We will see all the cases that have passed, the green color as in the image.

We intentionally modified the code to run the code wrong. Rerunning Unit Test, we will see that the cases have failed. Unit Test has automatically captured the program errors for you already. Click on each test case; we will see the desired results in the “Excepted,” the code results in the “But was.”

The purpose of the article is to guide you to start writing Unit Test for C # with NUnit so that I will stop here. Thank you and see you again.

Always be nice to anybody who has access to my toothbrush.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store