Monday, March 22, 2010

What is NUnit and How to Use it?

NUnit
NUnit is an open source unit testing framework built for the .NET Framework. NUnit allows you to write tests in the language of your choice to test a specific function of your application. Unit tests are an excellent way to test the functionality of your code when you first write it, and also to provide a method for regression testing of your application. The NUnit application provides a framework for writing unit tests, as well as a graphical interface to run these tests and view the results.

Writing an NUnit Test
As an example, I'm going to test the functionality of the Hashtable class in the .NET Framework to determine if two objects can be added and then retrieved. My first step will be to add a reference to the NUnit.Framework assembly, which will give me access to the attributes and methods of the NUnit framework. Next I'll create a class and mark it with the TestFixture attribute. This attribute lets NUnit know that this class contains NUnit tests:

using System; using System.Collections; 
using NUnit.Framework;  
namespace NUnitExample 
{     
[TestFixture]     
public class HashtableTest 
{         
public HashtableTest() 
{  }     
} 
Next I'll create a method and mark it with the [Test] attribute so that NUnit knows that this method is a test. Then I'll set up a Hashtable and add two values to it, then use the Assert.AreEqual method to see if I can retrieve the same values that I added to the Hashtable, as shown in the following:

[Test] public void HashtableAddTest() 
{     
Hashtable ht = new Hashtable();                  
ht.Add("Key1", "Value1");     
ht.Add("Key2", "Value2");      
Assert.AreEqual("Value1", ht["Key1"], "Wrong object returned!");     
Assert.AreEqual("Value2", ht["Key2"], "Wrong object returned!"); 
} 
This will confirm that I can add and then retrieve values from the Hashtable—a simple test, but one that showcases the capabilities of NUnit. There are a number of test types, as well as various Assert methods, that can be used to test every part of your code.

To run this test, I'll need to build the project, open the generated assembly in the NUnit application, and then click the Run button. Figure below shows the results. I get a warm and fuzzy feeling when I see that big green bar because it lets me know that the test passed. This simple example shows how easy and powerful NUnit and unit testing can be. Being able to write a unit test that can be saved and rerun whenever you change code not only makes it easier for you to detect defects in your code, but the result is that you can deliver better applications.
NUnit is an open-source project that is available for download from http://www.nunit.org. There is also an excellent NUnit Visual Studio .NET add-in which allows you to run unit tests directly from Visual Studio. This can be found at http://sourceforge.net/projects/nunitaddin

2 comments:

Adam said...

Thanks for this article, it was very useful and easy to understand. Please keep writing...

Ajay Gandhi said...

Nice blog, Sanjeev.

Yup, it is a wonderful open source tool that you could used for testing. I use it a lot. Also the cool feature about it is its Rollback attribute. First NUnit didn't have the rollback attribute for the test cases. So the changes made in the database could not be rolled back. But then they came up with this 'Rollback' attribute that could be used at the test level to rollback the changes you made to the database for your test case. Cool feature isn't it...