BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Testing
.NET 1.1+

NUnit Testing Framework

The second part of the Automated Unit Testing tutorial introduces NUnit. NUnit is a popular open source unit testing framework for .NET framework software that simplifies the process of creating, organising and executing automated unit tests.

Revisiting the Test Code with NUnit

With NUnit installed we can revisit the automated test code from the console application. To begin, create a new class library project and add the CommissionCalculator class to it. Next add another class library project to the solution. This project will contain the tests. It is a good idea to keep the tests in a separate project so that they are not included in any assembly that ships with your product.

The test project requires two references. In order that it can access the code under test, add a reference to the project containing the CommissionCalculator class. The second reference should be to the file, "nunit.framework.dll", which is part of the NUnit installation. This DLL contains the attributes and methods we will use for automated testing. To simplify the code, ensure you include the following using directive in your test classes.

using NUnit.Framework;

To complete the test project, add the code below to a new class named, "CommissionCalculatorTests". This code contains the test logic in public methods. Note the attributes used to identify tests and the Assert commands, which define the expected functionality. The use of these items will be described later in the tutorial.

[TestFixture]
public class CommissionCalculatorTests
{
    CommissionCalculator _calculator = new CommissionCalculator();

    [Test]
    public void Test999_99Sale()
    {
        Assert.AreEqual(25, _calculator.CalculateCommission(999.99));
    }

    [Test]
    public void Test1000_00Sale()
    {
        Assert.AreEqual(50, _calculator.CalculateCommission(1000));
    }

    [Test]
    public void Test9999_99Sale()
    {
        Assert.AreEqual(500, _calculator.CalculateCommission(9999.99));
    }

    [Test]
    public void Test10000Sale()
    {
        Assert.AreEqual(750, _calculator.CalculateCommission(10000));
    }
}

Running the Tests

We can now execute the test suite with the NUnit graphical user interface (GUI) test runner. First compile the solution. Next, execute the file, "nunit.exe", found in the NUnit installation folder. This will start the test runner. To identify the project to test, open the File menu and choose "Open Project..." Browse to the folder containing the compiled DLL that was generated by the test project and open this file. Make sure that you open the test DLL, not the DLL containing the CommissionCalculator class.

When the DLL is opened, NUnit shows a tree containing all of the tests it has identified. These are organised according to the assembly, namespace, class name and method name for each test. You can choose any point in the tree and click the Run button to execute the tests in that area. The image below shows the result of running all of the tests.

NUnit GUI Runner

The test runner shows lots of information. The large red bar below the Run button shows the status of the set of tests that was executed. Red indicates that there was at least one failure. The text beneath the bar tells us that there was a single passed test and three failures. The tree structure shows red cross icons next to the names of tests that have failed and green ticks for the one that passed. To the right of the tree is a list of the results of the failed tests. Unlike with the testing in the console application, each named test shows the expected value and the incorrect actual value. If you click on the results in this list, the box below updates to show a stack trace for the test.

With this information it is much easier to identify where the problems are in the code under test. The bugs can be resolved and the tests re-run until all pass and the large bar turns green. However, the test runner provides more information and more options for running tests than we have seen in this simple example. Some of the additional functionality will be described in later articles in this series.

13 March 2011