Unit testing using nUnit tool   Leave a comment

In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules,  procedures developed to solve the problem, are tested to determine whether they are fit for use.

The steps involved in unit testing are:

1) Documentation

2) What should be tested while unit testing

3) Automate where Necessary

Below is the sequence of testing that has to be done while developing software

>> Unit testing

>> Integration testing

>> System testing

>> Acceptance testing

In this article we will discuss the nUnit tool to do the unit testing

NUnit is an open source unit testing framework for Microsoft .NET. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.

To understand how it works, lets create a sample application, in below example I have created the sample class called Mathmetics with two functions “Add” and “Substraction”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mathmetics
    public class Maths
        public Maths() { 

        public int Add(int number1, int number2)
            return number1 + number2;        

        public int Subtraction(int number1, int number2)
            return number1 - number2;

In order to test the above class using the nUnit tool, let’s create another project “Window Class Library” application in our case, I have created the “MathmeticsTest” with below code.

Note*: Make sure you have downloaded the nUnit tool and added the reference of “nunit.framework.dll” in your project and also added the reference of the project contains above class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Mathmetics;

namespace MathmeticsTest
    class MathsTest
        Maths maths;

        public void SetupTest()
            maths = new Maths();

        public void AddTest() { 
            int intResult = maths.Add(1,2);
            Assert.AreEqual(3, intResult);

        public void SubtractionTest() {
            int intResult = maths.Subtraction(2,1);

        public void TearDownTest() {
            maths = null;

Before going further let’s understand the attributes used in Unit testing with nUnit tool.

TestFixtureSetUp: SetUp is generally used for initialization purposes. Any code that must be initialized or set prior to executing a test is put in functions marked with this attribute.

TestFixtureTearDown: This is an attribute that acts the opposite of [SetUp]. It means the code written with this attribute is executed last, this is generally used for cleanup purpose.

Test: This attribute is used to test the method.

Test with ExpectedException attribute: This attribute is used to test methods that need to throw an exception in some cases. The cases may be FileNotFoundException and others.



Ignore: This is the attribute which is used for the code that needs to be bypassed.

Now, compile the code written in the class library project. Then, open the bin folder of nUnit tool and execute the “nunit.exe” exe.

This will open an GUI, browse to the “MathmeticsTest.dll” created using above code.

Run the test, if everything goes fine then it will show in green color, if any test fail then it will be shown in red.


In this example, we are using “Assert.AreEqual” method from the framework to test the result.

These are the basic steps for using the NUnit framework for unit testing.


Posted January 4, 2015 by Izharuddin Shaikh in C#

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: