Wednesday, May 1, 2013

Test Driven Development - Part II - Working with NUnit


NUnit is a popular unit-testing framework for all .Net languages. It is xUnit based unit testing tool for Microsoft .NET. xUnit is a collection unit testing frameworks which  provides an automated solution with no need to write the same tests many times, and no need to remember what should be the result of each test

xUint is originally develped by Kent Beck. , the brain behind test driven development.

NUnit is an open source testing tool. It can be downloaded from www.NUnit.org  It is a stand alone application. So we can run the test any time without opening the Visual Studio Project.

Here is an simple example of doing a TDD in Visual Studio with NUnit

Open Visual Studio 2010/2012
Select File => New Project => Visual C# => Console Application
Enter Project Name 'TDDWithNunit' and press OK button
Add a class with name 'fileupload' to the project
Add the following code to the class


 public class fileupload  
   {  
     public bool ValidateFileName(string filename)  
     {  
       throw new NotImplementedException();  
     }  
   }  


Now we are about to start the coding for 'ValidateFileName'. But in test driven development, we have to write the unit test before writing actual development code. The above code is just enough to compile the project.

Add new project to the solution by selecting File => Add => New Project => Visual C# => Class Library
Enter the Project Name ' TDDWithNunit.Tests'
Rename the Class1.cs to "fileuploadTests.cs"

For each class we have to create corresponding unit testing class. For the readability it would be better to give the test class name as "<Class Under Test Name>Tests"

We need to add reference to the project under test to the unit testing project.  Right click References  and select Solution => Project => TddwithNUnit.

Add NUnit framework reference to the project from Assemblies => Extensions =>nunit.framework
Add the following code to the Test class.
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 using NUnit.Framework;  
 namespace TddwithNunit.Tests  
 {  
   [TestFixture]  
   public class fileuploadTests  
   {  
     [Test]  
     public void ValidateFileName_EmptyFilname_ReturnsFalse()  
     {  
       //Assign  
       fileupload file = new fileupload();  
       //Act  
       var result =file.ValidateFileName("");  
       //Assert  
       Assert.IsFalse(result, "Filename allows empty string");  
     }  
   }  
 }  

In the above code, [TestFixture] attribute has been added to the class and [Test] attribute has added to the testing method as well. This two attributes are required for the Nunit framework to detect the test class and methods. Naming of methods also need some attention. It should be "Public" and "void". It will be better to name the testing method in manner like __. It will help to find the issue easily even the method contains more number of methods. Now build the application. Load Nunit from the Start Menu Select File => Open Project =>\bin\Debug\ TddwithNunit.Test.dll





It will display the test class and method as in the above image Click the Run button


The method will fail because the method is not implemented. It will be indicated by the red line. Now modify the ValidateFileName method as below

  public bool ValidateFileName(string filename)  
     {  
       //throw new NotImplementedException();  
       if (filename != string.Empty)  
       {  
         return true;  
       }  
       else { return false; }  
     }  

Build the Application Run the NUnit Application. It will load the project automatically
Click the Run button

The test passed this time with the green signal. We can refactor the code now.

No comments:

Post a Comment