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+

Organising NUnit Tests

The fourth part of the Automated Unit Testing tutorial continues the examination of NUnit attributes. This article describes three attributes that can be used to categorise tests and determine which tests or fixtures are executed in the test runner.

NUnit Attributes

The previous article described several NUnit attributes that are used to define test fixtures and individual tests, and setup and teardown methods that prepare objects for testing and clean up after each test is executed. These are the basic attributes that are required for creating tests in an efficient manner.

In this article we will look at three new attributes that allow you to categorise or ignore tests. They allow you to determine which tests are executed during test runs. These attributes are not essential but can improve the testing process and speed.

Category Attribute

The first of the three attributes is Category. The Category attribute can be applied to tests or test fixtures to provide a category name for the decorated item. Once categorised, you can decide to run only the tests within specific categories or to omit certain sets of tests. Categories are particularly useful when trying to isolate long-running tests or integration tests that would otherwise slow the process.

The code below shows two tests in a single fixture. The second test has been made artificially slow by adding a ten second delay. The Category attribute has been applied to this test so that it can be omitted as required.

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

    [Test, Category("Slow Tests")]
    public void SlowTwoTimesTwoEqualsFour()
    {
        System.Threading.Thread.Sleep(10000);
        Assert.AreEqual(4, 2 * 2);
    }
}

Choosing the Categories to Test

The NUnit graphical test runner allows you to control which categories are executed. When the above test code is compiled and loaded into the test runner the "Slow Tests" category is identified automatically. This can be seen by selecting the Categories tab at the left of the list of tests.

NUnit Category Selection

The pictured section of the graphical user interface (GUI) allows you to identify one or more categories by selecting them in the list of available categories and clicking the Add button to move them into the selected categories area. If the "Exclude these categories" checkbox is checked, these tests will not be executed when you click the Run button. When the checkbox is cleared, only tests in the selected categories are executed.

Try executing the tests with and without the "Slow Tests" category excluded, switching back to the Tests tab to view the results. Note that the omitted tests are shown in grey. They also appear in the "Tests Not Run" list.

Applying Categories to Test Fixtures

You can apply the Category attribute to entire test fixtures. A test fixture category is added to every test within the fixture. If an individual test also includes a category, that test is added to both the test and test fixture categories. The sample code below shows two test fixtures. One has no category, the other is in the "Slow Tests" group.

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

[TestFixture, Category("Slow Tests")]
public class SimpleTestFixtureWithSlowTests
{
    [Test]
    public void SlowTwoTimesTwoEqualsFour()
    {
        System.Threading.Thread.Sleep(10000);
        Assert.AreEqual(4, 2 * 2);
    }
}
30 March 2011