i had spoken about TDD in one of my earlier posts.
now we can discuss about using some unit testing tools for TDD.One of the most common tools used is NUnit. the class which i want to test is a class which performs some basic math operations like add,subtract,divide and multiply.
the code for the class is as shown below
using System;
using System.Collections.Generic;
using System.Text;
namespace BasicMaths
{
public class Math
{
public Math()
{
}
public int Add(int i, int j)
{
return i + j;
}
public int Subtract(int i, int j)
{
return i - j;
}
public int Multiply(int i, int j)
{
return i * j;
}
public int Divide(int i, int j)
{
if(!CheckZero(j))
return i / j;
return 0;
}
public bool CheckZero(int i)
{
return( (i==0)? true:false);
}
}
}
now we have to give this class to some other component for consumption. But before that we have to make sure that each and every operation in this class works fine. For that we either have to write test stubs using NUnit. when we write a test class using NUnit we have to add NUnit.Framework.dll as a reference.Then we have to write the Test class.
As shown in the code below, the Class has to have an Attribute called [TestFixture], this is to convey to the compiler that the class is used for testing purpose. and each method which is used for testing should have the attribute [Test] to convey that the method is actually a NUnit test method.
we will build this project. a DLL will get created as a result. go to the folder where NUnit is installed. Run the file NUnit-GUI.exe. in this application, go to file->load rojet and then select the basicMathsTester.dll.
The DLL is loaded and all the classes along with their member functions are listed.
select on any function and click on the button run. If the method passes, then there will be aprogress bar in green color, else in case of failure the progress bar will be red in colour.