How to do Unit Test Using NUnit : Part 1

Unit Testing is an essential part of any production code. We can see the rise of Test Driven Development (TDD) approaches in many development projects. There are certain pros and cons of TDD but certainly we cannot ignore the TDD way of software development. I have read many blogs and tweets about TDD and became inspired to do some testing. I began learning to do unit testing of my own code. On searching I found NUnit, an open source Unit Testing framework. It is the optimum way to start with Unit Testing. I downloaded NUnit from here and started my testing journey. After successful installation you will get NUnit GUI shown in the following image:

UnitTest1.jpg

What to Unit Test?

Before we proceed in this article, let us discuss the code we are going to test. The decision of what to Unit Test and what not to Unit Test is very simple. If a unit of code contains any logic then we must do unit testing of that code.

UnitTest2.jpg

In this article we will test a basic function called DivideByZero. This function will return a false value if the divisor is equal to zero. The DivideByZero function is as follows:

UnitTest3.jpg

How to Unit Test?

To write our first Unit Test, we need to create a Test Project. To create a test project create a class library project. While writing the Unit Test we should follow a naming convention.

UnitTest4.jpg

Since the name of the project we are testing here is CalculatorLibraryToTest, hence we will create the test project with the name CalculatorLibraryToTest.Test. Next we will create a class with the name CalcLibraryTest since the function we will test is inside the class CalcLibrary.
 
Adhering to that naming convention we can create the test class as in the following:

UnitTest5.jpg

To tell NUnit explictity that this class is not a normal class rather than a test class, we need to attribute the class with [TestFixture]

As of now we have written class that is going to hold the NUnit tests. Next we need to write Unit Test. We are going to test function DivideByZero and to create Test function we will follow naming convention.

  1. We are verifying that the divisor parameter is zero. So this is appended as the second part of the test function.
  2. We are expecting that if the parameter passed is 0 then the expected result would be false. So this is appended as the third part of the function.

To tell NUnit explictly that this function is not a normal function rather than a test function, we need to attribute the function with [Test].

UnitTest6.jpg

Since we have the test function in place, let us go ahead and write the code to test. In the test we are creating an object of the class CalcLibrary and then calling the function with the parameters required.

using NUnit.Framework;

 

namespace CalculatorLibraryToTest.Test

{

 

    [TestFixture]

    public class CalcLibraryTest

    {

        [Test]

        public void DivideByZero_IsDivisorZero_ReturnsFalse()

        {

            CalcLibrary  testobject = new CalcLibrary();

            bool result = testobject.DivideByZero(56, 0);

            Assert.IsFalse(result, "divisor passed is 0 ");

 

        }

    }

}

In the code above we are using Assert. We will see in detail about Assert in future articles. Essentially we are asserting that the returned result should be false.

To perform the Unit Test go ahead and open the NUnit project editor and open an existing project. You need to provide the test assembly name to test. Here we will see that the unit test has passed.

UnitTest7.jpg

In this way we can perform unit testing using NUnit. In future articles we will get into the details of Unit Testing. I hope you find this article useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all