Wednesday, May 15, 2013

Test Driven Development - Part III - Microsoft Testing Tools

Microsoft has also developed their own testing tools. Since VS2005, It has been integrated with the IDE. One of the main advantage of using Microsoft testing tool is the debugging feature with it. But it is not a standalone application. So you need to open the project whenever you want to run the tests.

We can start by opening the same project we did in the previous example.
Add new project to the solution by selecting File => Add => New Project => Visual C# => Test => Unit Test Project.(In VS2010 we had an option to create unit test project by right clicking on the method and selecting 'Create Unit Tests...'  from the popup menu. In VS2012 that feature has been dropped.)

Enter the Name "TddwithNunit.Tests2" and press OK button
The Project will be added to the solution. The project will be referenced to "Microsoft.VisualStudio.QualityTools.UnitTestFramework " and the project will have the following code.

 using System;  
 using Microsoft.VisualStudio.TestTools.UnitTesting;  
 namespace TddwithNunit.Tests2  
 {  
   [TestClass]  
   public class UnitTest1  
   {  
     [TestMethod]  
     public void TestMethod1()  
     {  
     }  
   }  
 }  

Unlike Nunit, Microsoft uses [TestClass] and [TestMethod] attributes for Class and Methods respectively. Add reference to the TddwithNunit project. Rename the class file to "FileUploadTests" and modify the code as below.

 using System;  
 using Microsoft.VisualStudio.TestTools.UnitTesting;  
 namespace TddwithNunit.Tests2  
 {  
   [TestClass]  
   public class FileUploadTests  
   {  
     [TestMethod]  
     public void ValidateFileName_FilnameShorterthan6letters_ReturnsFalse()  
     {  
       //Assign  
       fileupload file = new fileupload();  
       //Act  
       var result = file.ValidateFileName("abc.txt");  
       //Assert  
       Assert.IsFalse(result, "Shorter Filenames are returning true");  
     }  
   }  
 }  


Now build the solution.

Select => Test =>Run =>All Tests . The result of the test will be displayed in Test Explorer inside the IDE as below.


The test has been failed. This logic has not been implemented into the ValidateFilename method. We have to refactor the code as below

 public class fileupload  
   {  
     public bool ValidateFileName(string filename)  
     {  
       bool returnvalue = false;  
       if (filename != string.Empty)  
       {  
         var filepart1 = filename.Split('.').First();  
         if (filepart1.Length >= 6)  
           returnvalue = true;  
       }  
       return returnvalue;  
     }  
   }  
 }  

And run the Test again from the Test Explorer => Run All


The test has been succeeded this time.

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.