How to set a free TDD-enabled environment



Download and install the following:

 

Testing framework:


   NUnit (version: 2.4.1 or newer)

 

Code Coverage tool:

 

   NCover (version 1.5.8 or newer)

 

Visual Studio .Net integration tool:

 

   TestDriven.Net (version 2.7.* or newer)

 



Making it play together:

 

First of all, let me apologize for the lame example, it’s kinda late for me to get creative.

Let’s create a Class Library named “Calculator.Library.Tests” and write the following test in it:


[TestFixture]
public class CalculatorTests
{
   [Test]
   public void Divide_TwoValidNumbers()
   {
      Calculator c = new Calculator();

      int expected = 3;
      int actual = c.Divide(6, 2);

      Assert.AreEqual(expected, actual);
   }
}


 

Now we’ll create another project(Class Library) named “Calculator.Library” and write the following code in it:


public class Calculator
{
   public int Divide(int a, int b)
   {
      if (b == 0)
         throw new DivideByZeroException(“err”);

      return a / b;
   }
}




Quick run of our test (print to console output):


Put the cursor in the test-method(Add_TwoValidNumbers) or in the test-class(CalculatorTests) -> right-click -> Run Test(s).


Run tests and see results in a “nice”(depends on your definition for nice) UI:

 

Right-click on the project “Calculator.Library.Tests” -> Test With -> NUnit 2.4

 

View Code Coverage:

 

Right-click on the test-method\test-class\test-project -> Test With -> Coverage. A new application named NCoverExplorer will be open – there you could explore the coverage of the code. As default, you’ll see the coverage in your tests as well which is not interesting. This can be easily fixed by changing the settings in NCoverExplorer -> View -> Options… -> Exclusions (Tab) -> pattern: “*.Tests” -> Add.

 

You can see that we have 75% coverage at the moment:

 

ncoverexplorer.JPG

 

 

We can now add the following test to check the missed path (and then simply call Test With -> Coverage again):


[Test]
[ExpectedException(typeof(DivideByZeroException))]
public void Divide_TryToDivideByZero_ThrowsException()
{
   Calculator c = new Calculator();
   c.Divide(6, 0);
}


 

Easy, Fast, Free and (almost) Fully Integrated in my development environment.
Life is pretty good.