Unit testing using nUnit tool

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
{
    [TestFixture]
    class MathsTest
    {
        Maths maths;

        [TestFixtureSetUp]
        public void SetupTest()
        {
            maths = new Maths();
        }

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

        [Test]
        public void SubtractionTest() {
            int intResult = maths.Subtraction(2,1);
            Assert.AreEqual(1,intResult);
        }

        [TestFixtureTearDown]
        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.

e.g.

[Test]
[ExpectedException(typeof(MissingFileException))]

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.

nUnit

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.

Advertisements

#nunit-tool, #unit-testing