Unit Testing is essential part of any production code. We can see rise of Test Driven Development (TDD) approach in many development projects. There are certain pros and cons of TDD but certainly we cannot ignore TDD way of software development. I read many blogs and tweets about TDD and got inspired to do some testing. I started learning to do unit testing of my codes. On searching I found NUnit an open source Unit Testing framework 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 below image,
What are we going to do Unit Test?
Before we move ahead in this blog post, let us discuss the code we are going to test. What to Unit Test and what not to Unit Test decision making is very simpler. If unit of code contains any logic then we must do unit testing of that code.
In this blog post we are going to test a basic function called DivideByZero. This function will return a false value if divisor is equal to zero. DivideByZero function is as following
How to do Unit Test ?
To write first Unit Test, we need to create a Test Project. To create a test project create a class library project. While writing Unit Test we should follow naming convention.
Since name of the project we are testing here is CalculatorLibraryToTest hence we will create test project with name CalculatorLibraryToTest.Test . Next we will create a class with the name CalcLibraryTest since function we are going to test is inside the class CalcLibrary.
Adhering to naming convention we can create test class as 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 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.
- We are verifing that divisor parameter is zero or not . So this appened as second part of the test function.
- We are expecting if passed parameter is 0 then expected result would be false. So this is appened as third part of the function.
To tell NUnit explictly that this function is not a normal function rather a test function , we need to attribute the function with [Test]
Since we have test function is in place let us go ahead and write the code to test . In test we are creating object of the class CalcLibrary and then calling the function with required parameters.
public class CalcLibraryTest
public void DivideByZero_IsDivisorZero_ReturnsFalse()
CalcLibrary testobject = new CalcLibrary();
bool result = testobject.DivideByZero(56, 0);
Assert.IsFalse(result, "divisor passed is 0 ");
In above code we are using Assert. We will see in detail about Assert in further blog posts. Essentially we are asserting that returned result should be false.
To perform Unit Test go ahead and open NUnit project editor and open exsiting project. You need to provide test assemly name to test. Here we can see that unit test is passed.
In this way we can perform unit testing using NUnit. In further posts we will get into details of Uni Testing. I hope you find this post useful. Thanks for reading.