What is Unit Testing in .Net

Unit testing is a kind of testing done at the developer side. It is used to test methods, properties, classes, and assemblies. Unit testing is not testing done by the quality assurance department. To know where unit testing fits into development, look at the following image:
Unit testing is used to test a small piece of workable code (operational) called unit. This encourages developers to modify code without immediate concerns about how such changes might affect the functioning of other units or the program as a whole. Unit testing can be time consuming and tedious, but should be done thoroughly with patience.

What is NUnit?
NUnit is a unit testing framework for performing unit testing based on the .NET platform. It is a widely used tool for unit testing and is preferred by many developers today. NUnit is free to use. NUnit does not create any test scripts by itself. You have to write test scripts by yourself, but NUnit allows you to use its tools and classes to make unit testing easier. The points to be remembered about NUnit are listed below:

NUnit is not an automated GUI testing tool.
It is not a scripting language, all tests are written in .NET supported languages, e.g., C#, VC, VB.NET, J#, etc.
NUnit is a derivative of the popular testing framework used by eXtreme Programming (XP). It was created by Philip Craig for .NET. It is also available in the name of jUnit for Java code testing.

NUnit works by providing a class framework and a test runner application. They can be downloaded from here. The class framework allows to write test cases based on the application. The test is run using the test runner application downloaded from the above link.

Steps for Using NUnit
First, what one needs to do is download the recent version of the NUnit framework from the above mentioned website.

In development studio, create a new project. In my case, I have created a console application.
In the program.cs file, write the following code:

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter two numbers\n");
int number1;
int number2;
number1 = int.Parse(Console.ReadLine());
number2 = int.Parse(Console.ReadLine());

MathsHelper helper = new MathsHelper();
int x = helper.Add(number1, number2);
Console.WriteLine("\nThe sum of " + number1 + 
" and " + number2 + " is " + x);
Console.ReadKey();
int y = helper.Subtract(number1, number2);
Console.WriteLine("\nThe difference between " + 
number1 + " and" + number2 + " is " + y);
Console.ReadKey();
}
}

public class MathsHelper
{
public MathsHelper() { }
public int Add(int a, int b)
{
int x = a + b;
return x;
}

public int Subtract(int a, int b)
{
int x = a - b;
return x;
}
}

Then to the solution of the project, add a new class library project and name it followed by “.Test” (it is the naming convention used for unit testing). Import the downloaded DLL files into the project and follow the steps given below.
In the newly added project, add a class and name it TestClass.cs.
In the class file, write the following code:

public class TestClass
{
[TestCase]
public void AddTest()
{
MathsHelper helper = new MathsHelper();
int result = helper.Add(20, 10);
Assert.AreEqual(30, result);
}

[TestCase]
public void SubtractTest()
{
MathsHelper helper = new MathsHelper();
int result = helper.Subtract(20, 10);
Assert.AreEqual(10, result);
}
}

Now open the test runner (test runner is downloaded from the NUnit site along with the NUnit DLLs). In the NUnit API, click File > Open project. A file open dialog appears. Give the path of the NUunit test project DLL. Now run the test. If the test passes, then the following test screen is displayed:

 


Discover more from CODE t!ps

Subscribe to get the latest posts sent to your email.

Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading