Fundamentals of Unit Testing: Unit Test Using Nunit

This is the “Fundamentals of unit testing” article series. In our previous article we saw various concepts of unit testing, you can read them here.

In our previous articles we have implemented unit testing in Visual Studio's built-in unit test application project, but in this article we will use the Nunit framework to implement the unit test. The Nunit framework is very popular and simple to use.

So, to use Nunit framework, we need to install it in our system. Go to the following URL and download the Nunit framework. There are many download options. I have chosen .msi because I am using Windows and wanted to install using a direct installer file.

installer file

Once you have finished the installation you will find Nunit application in your All program menu. Fine, now we will create a class library application that we will implement as the unit test application using Nunit and one more class library application that we will test using Nunit test.

So, let's create the class library and make it the unit test application.
class library

I have used the name N-Unit for the application. Just click OK to create it. Now, we will install the Nunit framework to this project from the Nuget package manager.
Nuget package manager

Once installed, you can provide reference of the “nunit.framework” library in the application, now the application will behave as a unit test application.
Test application

Ok, fine; let's write a class to implement the unit test. Here is the sample class that will test a calculator class that we will implement shortly.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using NUnit.Framework;  
  7. using TestProjectLibrary;  
  8. namespace N_Unit  
  9. {  
  10.     [TestFixture]  
  11.     public class testClass  
  12.     {  
  13.         calculator objc = new calculator();  
  14.         [TestCase]  
  15.         public void Addtest()  
  16.         {  
  17.             Assert.AreEqual(20, objc.Add(10, 10));  
  18.         }  
  19.         [TestCase]  
  20.         public void Subtest()  
  21.         {  
  22.             Assert.AreEqual(0, objc.Sub(10, 10));  
  23.         }  
  24.         [TestCase]  
  25.         public void Multest()  
  26.         {  
  27.             Assert.AreEqual(100, objc.Mul(10, 10));  
  28.         }  
  29.         [TestCase]  
  30.         public void Divtest()  
  31.         {  
  32.             Assert.AreEqual(1, objc.Div(10, 10));  
  33.         }  
  34.     }  
  35. }  


And here is the calculator class that we want to test using unit test application.
  1. namespace TestProjectLibrary  
  2. {  
  3.     public class calculator  
  4.     {  
  5.         public int Add(int a,int b)  
  6.         {  
  7.             return a + b;  
  8.         }  
  9.         public int Sub(int a, int b)  
  10.         {  
  11.             return a - b;  
  12.         }  
  13.         public int Mul(int a, int b)  
  14.         {  
  15.             return a * b;  
  16.         }  
  17.         public int Div(int a, int b)  
  18.         {  
  19.             return a / b;  
  20.         }  
  21.     }  
  22. }  


Fine, everything is set up, now build the unit test application. If it's error-free then we are fine and we will execute a test. I am assuming that the application was built successfully.

Now, open the Nunit application to run the test. You will find it in All programs as in the following:

Nunit application

Once the application opens, click on File New application and load the DLL of the unit test application. Please be careful to select the path of the unit test application and load the proper DLL.

Once it has loaded the test, you will find the following screen. We are seeing that all the tests have been loaded successfully.

Now press the run button to run all tests.
run all test

Cheers! Our all test has passed.


This is the procedure to run a unit test using the Nunit framework. This is another way to do a unit test other then the Visual Studio built-in test framework. Though they have little pros and cons in both, we will discuss them in another article.

Similar Articles