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.

Ignore Attribute

Sometimes you will wish to temporarily disable one or more tests, perhaps because the tests or the code under test are not ready for testing but your attention is needed elsewhere in the solution. You could comment out the tests but this raises the possibility that you forget to replace them later. You could also use the Category attribute to create a group of omitted tests. However, NUnit provides a simpler way to disable tests, using the Ignore attribute.

When tests are ignored, they are not executed by the test runner. They do appear in the GUI and are marked with a yellow exclamation icon when a test run would otherwise have executed them. When a test run is completed that includes ignored items and tests that fail, the progress bar appears red as normal. If all tests pass, the progress bar is coloured yellow so that you do not forget the ignored tests. The skipped tests also appear in the "Tests Not Run" list.

The following test fixture includes a single test that is decorated with the Ignore attribute.

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

When the test is executed, the progress bar appears yellow:

NUnit Ignore Attribute

If you ignore many tests for different reasons, you can provide a reason for skipping the tests as an argument of the attribute. The reason is displayed in the "Tests Not Run" list. The following code includes a test fixture that is ignored with a reason specified.

[TestFixture, Ignore("Tests not valid in v2.0")]
public class SimpleTestFixture
{
    [Test]
    public void TwoTimesTwoEqualsFour()
    {
        Assert.AreEqual(4, 2 * 2);
    }
}

Explicit Attribute

When a test is decorated with the Ignore attribute it will be omitted from test runs. Sometimes you will want to ignore tests but have the option of executing them occasionally without modifying the code. Tests and test fixtures decorated with the Explicit attribute are not executed as part of a larger set of tests. However, if you select these tests in the GUI and click the Run button they are executed individually.

As with the Ignore attribute you can provide an optional reason. The following sample ignores a test fixture unless it is explicitly selected for execution. A reason has been supplied.

[TestFixture, Explicit("Tests not valid in v2.0")]
public class SimpleTestFixture
{
    [Test]
    public void TwoTimesTwoEqualsFour()
    {
        Assert.AreEqual(4, 2 * 2);
    }
}

The output of skipped tests marked as Explicit is different to that of ignored tests. The items still appear in the test list but are not highlighted with the exclamation mark icon. The progress bar is also unaffected, so if all other tests pass, the bar is coloured green.

Try executing the above test code in the NUnit graphical test runner with the test selected and not selected to see the difference.

30 March 2011