BlackWaspTM
Testing
.NET 1.1+

Creating Unit Tests

The third part of the Automated Unit Testing tutorial examines the construction of unit tests using the NUnit testing framework. This article describes the use of the attributes that determine how the test runner executes unit test code.

NUnit Attributes

In the previous article in this tutorial I described some basic tests created and executed using the NUnit testing framework. In this article we will examine the structure of the unit test code further, concentrating on attributes that are used to identify sections of code as tests or as methods that prepare objects for testing and clean up after tests are executed. As before, the syntax described is specific to NUnit. However, the techniques are similar to those used in most popular .NET testing frameworks.

To keep the examples in the article as simple as possible, we will run tests using existing classes and structures from the .NET framework. It is likely that there would be no reason for you to test .NET classes in reality but this will allow us to concentrate on the tests rather than the code under test. To begin, create a new class library project. To provide easy access to the NUnit framework, add a reference to the nunit.framework.dll assembly and include the following using directive in all source code files containing tests:

using NUnit.Framework;

A Simple Test

All tests use the same underlying structure. Firstly you must create a test fixture. This is a container that can hold a single test or a group of related tests. When using NUnit, a test fixture is a class that is decorated with the TestFixture attribute. The class must be declared with a public scope and must be concrete, not abstract. NUnit creates instances of test fixture classes using the default constructor so this must be present, either by being defined explicitly or by the test fixture containing no constructors. The test runner may instantiate the class several times so the constructor should have no side-effects. It is advisable in most cases to omit constructors from the class entirely.

Within a test fixture class you create one or more tests. Each test is a public method that is decorated with the Test attribute. In order that the test is executed the method must have the correct signature. It should have no parameters and a void return value.

Each unit test should exercise a small section of the code under test. Usually a test will include an assertion, which specifies an expected value after the code is executed. Assertions are defined using static methods from the Assert class. Various types of assertion will be described in a later article in the series.

To create a simple test, add the following class to the project.

[TestFixture]
public class SimpleTestFixture
{
    [Test]
    public void TwoTimesTwoEqualsFour()
    {
        Assert.AreEqual(4, 2 * 2);
    }
}

The above class is a test fixture named SimpleTestFixture. It contains a single test, named TwoTimesTwoEqualsFour. The name of the method describes the purpose of the test. In this case we are using the Assert.AreEqual method to define an assertion. The first argument of the method specifies that we expect a value of four. The second argument provides that actual value for comparison. In this case the value is the expression, 2*2. If the values match and the method exits normally, the test runner will indicate a successful test. If the two values do not match or an exception is thrown during execution, the test fails.

23 March 2011