Introduction To NUnit Testing Framework


Every successful software development follows a lifecycle known as SDLC (Software Development Life Cycle).As per Wikipedia:

"SDLC is a process followed for a software project, within a software organization. It consists of a detailed plan describing how to develop, maintain, replace and alter or enhance specific software. The life cycle defines a methodology for improving the quality of software and the overall development process."

As we are a developer we normally understand the requirements and write the code. Apart from those we also spend some time software testing.

Software testing is a process of executing a program or application with the intent of finding the software bugs.

It can also be described as the process of validating and verifying a software program or application or product.

I have Googled and tried to find out what kind of testing a developer can do. I got lot of solutions from the internet, and  the best one I found is here. So mainly a developer must have knowledge of Unit Testing.

What is unit Testing?

Let's try to find the exact definition of Unit.


In this IT world a unit refers to simply a smallest piece of code which takes an input ,does certain operation, and gives an output.
And testing this small piece of code is called Unit Testing.

A lot of unit test frameworks are available for .Net nowadays, if we check in Visual Studio we have MSTest from Microsoft integrated in Visual Studio.

Some 3rd party frameworks are:
  • NUnit
  • MbUnit
Out of all these Nunit is the most-used testing Framework.

What Is NUnit?

NUnit is a unit-testing framework for all .Net languages.

NUnit is Open Source software and NUnit 3.0 is released under the MIT license. This framework is very easy to work with and has user friendly attributes for working.

You can check the details of Nunit from here. 

If you want to learn what are the main differences between MsTest and Nunit I recommend you check the following Link. 

You can also check several other links that are available on the  internet. I did a small inspection on internet and found the following result in using different Testing frameworks. As per the Slant website, this is what most people recommend on using different unit Testing frameworks. 

To work with Nunit let's set up a simple console application as follows.

Write the following class in this project.
  1. namespace DemoProject {  
  2.     public class EmployeeDetails {  
  3.         public int id {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public double salary {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Geneder {  
  16.             get;  
  17.             set;  
  18.         }  
  19.     }  
  20. }  
Now in the class program write some conditions which need to be tested.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace DemoProject {  
  7.     public class Program {  
  8.         public string Login(string UserId, string Password) {  
  9.             if (string.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(Password)) {  
  10.                 return "Userid or password could not be Empty.";  
  11.             } else {  
  12.                 if (UserId == "Admin" && Password == "Admin") {  
  13.                     return "Welcome Admin.";  
  14.                 }  
  15.                 return "Incorrect UserId or Password.";  
  16.             }  
  17.         }  
  18.         public List < EmployeeDetails > AllUsers() {  
  19.             List < EmployeeDetails > li = new List < EmployeeDetails > ();  
  20.             li.Add(new EmployeeDetails {  
  21.                 id = 100, Name = "Bharat", Geneder = "male", salary = 40000  
  22.             });  
  23.             li.Add(new EmployeeDetails {  
  24.                 id = 101, Name = "sunita", Geneder = "Female", salary = 50000  
  25.             });  
  26.             li.Add(new EmployeeDetails {  
  27.                 id = 103, Name = "Karan", Geneder = "male", salary = 40000  
  28.             });  
  29.             li.Add(new EmployeeDetails {  
  30.                 id = 104, Name = "Jeetu", Geneder = "male", salary = 23000  
  31.             });  
  32.             li.Add(new EmployeeDetails {  
  33.                 id = 105, Name = "Manasi", Geneder = "Female", salary = 80000  
  34.             });  
  35.             li.Add(new EmployeeDetails {  
  36.                 id = 106, Name = "Ranjit", Geneder = "male", salary = 670000  
  37.             });  
  38.             return li;  
  39.         }  
  40.         public List < EmployeeDetails > getDetails(int id) {  
  41.             List < EmployeeDetails > li1 = new List < EmployeeDetails > ();  
  42.             Program p = new Program();  
  43.             var li = p.getUserDetails();  
  44.             foreach(var x in li) {  
  45.                 if ( == id) {  
  46.                     li1.Add(x);  
  47.                 }  
  48.             }  
  49.             return li1;  
  50.         }  
  51.         static void Main(string[] args) {}  
  52.     }  
  53. }  
So here I have written 3 methods or we can say some requirements which we want to test by using Unit Test. So please add a new project (Class Library Type) in the Solution Explorer as follows.

The Nunit framework does not require any specific project type, but most of the time people will add a class library to separate their code from their unit tests. You need to reference the nunit.framework.dll yourself. But if you use nuget this will be done for you automatically while installing NUnit.

So for using Nunit we need to go for Nuget Package Manager and download it.


Now after installing the Nunit we need one more dll that needs to be installed in the project.

NUnit Test Adapter for Visual Studio

The NUnit Test Adapter allows you to run NUnit tests inside Visual Studio. 


If you do not add this component in your project you will not be able to find your tests in test Explorer.


You need to install both the Libraries in the project where you are writing the test methods only.

Before writing any test case let's be aware of all the components we are using in Nunit. If you check the Nunit site you will get all the attributes and other component details there.

Now our next job is to add the Demo Project dll in DemoProjectTest References.

Now it will add the following dll to the references.

So here is the test case I  have written to test our requirements.
  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 DemoProject;  
  8. namespace DemoProjectTest {  
  9.     [TestFixture]  
  10.     public class DemoTests {  
  11.         List < EmployeeDetails > li;  
  12.         [Test]  
  13.         public void Checkdetails() {  
  14.                 Program pobj = new Program();  
  15.                 li = pobj.AllUsers();  
  16.                 foreach(var x in li) {  
  17.                     Assert.IsNotNull(;  
  18.                     Assert.IsNotNull(x.Name);  
  19.                     Assert.IsNotNull(x.salary);  
  20.                     Assert.IsNotNull(x.Geneder);  
  21.                 }  
  22.             }  
  23.             [Test]  
  24.         public void TestLogin() {  
  25.                 Program pobj = new Program();  
  26.                 string x = pobj.Login("Ajit""1234");  
  27.                 string y = pobj.Login("""");  
  28.                 string z = pobj.Login("Admin""Admin");  
  29.                 Assert.AreEqual("Userid or password could not be Empty.", y);  
  30.                 Assert.AreEqual("Incorrect UserId or Password.", x);  
  31.                 Assert.AreEqual("Welcome Admin.", z);  
  32.             }  
  33.             [Test]  
  34.         public void getuserdetails() {  
  35.             Program pobj = new Program();  
  36.             var p = pobj.getDetails(100);  
  37.             foreach(var x in p) {  
  38.                 Assert.AreEqual(, 100);  
  39.                 Assert.AreEqual(x.Name, "Bharat");  
  40.             }  
  41.         }  
  42.     }  
  43. }  

The class that is to be tested using Nunit should be decorated with TextFixture.


This attribute identifies the method to be tested. If we do not write this attribute then we can't to identify the test in Testexplorer. We have an Assert class with the following methods for validating different conditions in the TestFixture.

Each methods has different functions.To know about this please check the Nunit Home Page.

Now let's check our test. Go to the Visual Studio Test Explorer.

Now if we check the Left side of Visual Studio it will open a pane called Test Explorer with all the test scenarios as follows.

Now from here if we run a test individually just right click on it and run the test

So in this way we can run the tests.

If the test Runs successfully it will show the following tick mark.


Beside these attribute we have some more attributes like,
  1. SetUp

    This attribute is used when you want to execute a piece of code in each test case. It identifies a method to be executed each time before a TestMethod/Test is executed. Let's check this with an example. Here I have created a new project for testing. Here I have created four test methods.

    Now let's check the methods.

    So here I have the methods where we have used setUp attribute. Let's try to debug and check SubMethod. For debugging just Right Click and Debug selected Test.

    Now if we notice the sequence it will go to the Addmethod first like these.

    Thus the method where the setup attribute is written will be executed first before any test method. 

  2. TearDown

    After completely executing each test if you want to execute a piece of code then you have to write this code under TearDown attribute.

    So let's check this again.S o we have set breakpoints to check the sequence of execution.

    If we check the sequence of execution the method that used TearDown attribute will be executed last after execution of the Test case.


If two SetUp classes are there the class that was written first will be executed first and then after that, the class that is written next to it will execute. (Top to bottom approach).

Similarly, in tear down the order is reversed -- it will follow bottom to top approach.

So in this way we can test our requirements using Nunit Testing. I will explain about  all the remaining attributes in my next article. Hope this article will help you to learn the basic concepts of Nunit.

Thanks for reading this article and Happy New Year to all.