How and Why Test Driven Development (TDD) and NUnit?

In my very first post, titled Some Fix Time Data, I showed statistics that demonstrate that the earlier you find bugs, the cheaper the cost. It’s not the only reason for believing in TDD, but it’s pretty compelling.

TDD helps in a number of ways. It helps you find bugs earlier. It helps when making regression changes. It helps when you are trying to determine what the functionality is that you are trying to implement. It improves quality, and provides that quality earlier. It helps you concentrate only on the needs of the application, and thus spend less time doing undesireable work.

By forcing us to write tests before implementing the meat of the application, we are being clear about the requirements. If the requirements are not clear, the tests cannot be written, and the tests themselves are a representation of those requirements. So if given a task, and the task is ambiguous, you will be able to demonstrate any problems much earlier.

Perhaps the simplest way to start with TDD, is to download NUnit from http://www.nunit.org. NUnit is free, and has a graphical user interface that enables you to build tests to test your objects. Note here that TDD isn’t actually about testing the User Interface itself.  There are other strategies for that. TDD is about building your business logic layer, and having a set of tests that allow for regression.

The first thing to do after installing NUnit is to open a project. The project can be either a new project or an existing project. You can add tests to an existing project, or add a new project specifically for testing. I prefer the second.

Next, you need to add a testing class. This is called a test fixture. The method calls within a testing class are, funnily enough, the tests. NUnit recognises test fixtures and tests by attributes added to the testing class, as follows.

[TestFixture]
public class MyTestClass
{
   [Test]
   public void MyTest()
   {
      //perform test code here
   }
}

NUnit recognises test fixtures by the [TestFixture] attribute added to the testing class. The tests themselves are also identified by the [Test] attribute. Note also that all tests must be public void and have no parameters. Also, you need to add a reference to NUnit.Framework in your project, and add the Imports NUnit.Framework statement in your class so that it can reference NUnit methods.

You write your test and at the end, use Assert statements to check the validity of the test. Here’s an example that adds two numbers:

using System;
using NUnit.Framework;
namespace MyNamespace
{
   [TestFixture]
   public class MyTestFixtureClass
   {
      [Test]
      public void CheckIfAdditionWorks()
      {
         int firstNumber = 1;
         int secondNumber = 2;
         int result = firstNumber + secondNumber;
         Assert.AreEqual(result,3);
      }
   }
}

When the test runs, this test will succeed, and the NUnit GUI will show a green bar. Green indicates success, Red failure, and Yellow for ignored tests. So go ahead and run NUnit. In the NUnit GUI, select File from the menu, and Open Project. Add the assembly or assemblies that contain your test fixture classes. Make sure you try this out first, as the following step won’t work unless you do it.

Next, you need to go into Project Properties (I’m using VS2008 here). Select your project in the Solution Explorer, right click, then select Properties. Choose the Debug item in the left navigation bar. In the Start Action group, select start external program and set it to: “C:\Program Files\NUnit 2.4.7\bin\nunit.exe”. This will now execute the NUnit program when you run the application from within Visual Studio. In the Start Options group, add the following settings “/run /fixture=MyNamespace.MyTestFixtureClass”.

The additional command line arguments basically force NUnit to immediately run the required tests. The /run argument without the /fixture argument will simply run all tests. With the /fixture argument specified with the name of a test fixture class, it will execute only the tests within that class. This should get you going.

And if you need to get more sophisticated, you can even choose to include or exclude categories of tests. NUnit can also be set up to use config files.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: