Post

# Getting Started With Unit Testing Using NUnit : Part 1

• 28.9k
• 0
• 4

Unit Testing is an essential part of any production code. We can see the rise of the Test Driven Development (TDD) approach 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 got inspired to do some testing. I started learning how to do unit testing of my 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 the NUnit GUI as shown in the following image:

What to do with Unit Test

Before we proceed in this article, let us discuss the code we will test. What to Unit Test and what not to Unit Test; the decision making is much simpler. If a unit of code contains any logic then we must do unit testing of that code.

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:

How to do the Unit Test

To write the 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 the naming convention.

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 the naming convention, we can create the test class as in the following:

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

As of now we have written a class to hold the NUnit tests. Next we need to write the Unit Test. We will test the function DivideByZero and to create the Test function we will follow the 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 passed parameter 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].

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

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 further articles. Essentially we are asserting that the returned result should be false.

To perform the Unit Test, proceed to open the NUnit project editor and open the existing project. You need to provide the test assembly name to test. Here we can see that the unit test has passed.

In this way we can perform unit testing using NUnit. In further articles we will get into the details of Unit Testing.