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.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using NUnit.Framework;

using TestProjectLibrary;

 

namespace N_Unit

{

    [TestFixture]

    public class testClass

    {

        calculator objc = new calculator();

 

        [TestCase]

        public void Addtest()

        {

            Assert.AreEqual(20, objc.Add(10, 10));

        }

 

        [TestCase]

        public void Subtest()

        {

            Assert.AreEqual(0, objc.Sub(10, 10));

        }

 

        [TestCase]

        public void Multest()

        {

            Assert.AreEqual(100, objc.Mul(10, 10));

        }

 

        [TestCase]

        public void Divtest()

        {

            Assert.AreEqual(1, objc.Div(10, 10));

        }

    }

}

 

And here is the calculator class that we want to test using unit test application.

namespace TestProjectLibrary

{

    public class calculator

    {

        public int Add(int a,int b)

        {

            return a + b;

        }

        public int Sub(int a, int b)

        {

            return a - b;

        }

        public int Mul(int a, int b)

        {

            return a * b;

        }

        public int Div(int a, int b)

        {

            return a / b;

        }

    }

}

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.
 
test

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

Cheers! Our all test has passed.

Conclusion

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.