Test-driven development with NUnit

NUnit

What's needed here is a framework that allows us to write tests and not worry too much about how we're going to get back their results. The de-facto framework for .Net unit testing is NUnit. Currently at version 2.1, NUnit provides us with a set of base classes and attributes that allow us to abstract away our unit tests and concentrate on the code that actually does the testing. The beautiful thing is that moving from our current coding/testing style to Nunit style requires little learning and is very easy to master.

Nunit allows us to separate out testing code into what is logically know as tests, test fixtures and test suites. The concept is very simple.  

  • You write a test to test a single piece of functionality.
  • You group one or more tests inside a test fixture that allows you to easily add repeatable state for each test (I'll explain shortly)
  • You group one or more fixtures inside test suites to logically separate the test and their meaning

So how do we turn our code into using Nunit style?

  • Download and install Nunit
  • Add a reference to the nunit.framework.dll to our testing project
  • Add a using clause for NUnit . Framework workspace in a new class file

Now we're ready to start working.

Change the class's name to MyTestingClass .

This class will hold the fixture for our tests. We also need to let the Nunit framework know that this class is a fixture, so we simple add a [ TestFixture ] attribute on top of the class name.  You can remove the default constructor from the class (but don't make it private!). Once we've done that, we have a class that looks like this:

[TestFixture]
public class MyTestClass
{
}

Easy enough we just have to start adding tests to the class. We'll use the code from our previous example to test against Calculator .

A test in a fixture is defined as a public method that returns void and accepts no parameters, marked with the [ Test ] attribute and with one or more assertions inside. So let's add the first test.:

[TestFixture]
public class MyTestClass
{

      [Test]
      TestAddition()
      {
//test addition
            int retVal = calc.Add(22,22);
            Assert.AreEqual(44,retVal,
"calc.Add() returned the wrong number");
}
}

As you can see it's the same code as before only now it's sitting in a method of its own .The method is decorated with the [Test] attribute .Also, instead of having to manually throw an exception I'm using the Assert class which is part of the Nunit framework. You'll get to know this class a lot as this is the main instrument you'll use to verify your code. The Assert class will fail the current test if the condition passed to it is false. It contains only static methods that allows you to make sure that a value is not null, equals other values and just send in any Boolean expression you like. You can also send in messages that explain the meaning of this failure.

Now we need to build our testing project so we can move on to the next step.

Test suites

Test suites in the new versions of Nunit are derived directly from the namespaces that the test fixtures reside in. If you have two fixtures in separate namespaces (i.e. one is not contained inside the other) they'll be considered as residing in two separate test suites.

You might also like...

Comments

About the author

Roy Osherove Israel

Roy Osherove has spent the past 6+ years developing data driven applications for various companies in Israel. He's acquired several MCP titles, written a number of articles on various .NET topic...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup