How to do Unit Test using NUnit : Part 1

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,

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.

image

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

image

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.

image

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 ,

image

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.

  1. We are verifing that divisor parameter is zero or not . So this appened as second part of the test function.
  2. 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]

 

image

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.


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 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.

image

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.

About these ads

6 thoughts on “How to do Unit Test using NUnit : Part 1

  1. Pingback: Dew Drop – January 17, 2013 (#1,481) | Alvin Ashcraft's Morning Dew

  2. Pingback: Dew Drop – January 18, 2013 (#1,482) | Alvin Ashcraft's Morning Dew

  3. Pingback: Линкблог #12

  4. Pingback: Test SetUp and TearDown in Unit Testing using NUnit : Part 2 « debug mode......

  5. Pingback: Ignore Test in Unit Testing using NUnit: Part 3 « debug mode......

  6. Pingback: TestFixtureSetUp and TestFixtureTearDown in Unit Testing using NUnit: Part 4 « debug mode......

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s