Printed from A Developer website designed for Developers

NUnit: Writing Test Code


* Based on NUnit version 2.4.8

Test code follows a standard formula:

Test Attributes

Each class that contains tests must be annotated with a [TestFixture] attribute. The class must be declared public (so that the test runners will run it; by default, classes are internal), and it must have a public, no-parameter, constructor.

Create individual methods annotated with [Test] attributes. Any public, parameterless method specified with a [Test] attribute will be run automatically by NUnit. You can include helper methods to support clean code in our tests as well, just don't mark them with a [Test] attribute.

NUnit lets you specify two methods to set up ([Setup] attribute) and then tear down ([TearDown] attribute) the environment for each one of the [Test] methods, executed, even if the test method throws an exception.

This can also be set up per TestFixture class, using the [TestFixtureSetup] and [TestFixtureTearDown] attributes. NUnit doesn't recreate the TestFixture class each time it runs a test and it discovers and runs the methods using reflection, so using the class Constructors would not work as expected.

[TestFixture] public void PerFixtureSetup() { ... } [TestFixtureTearDown] public void PerFixtureTeardown() { ... }

While setup and teardown methods generally come in pairs, they don't have to. Very often, a fixture will have a setup, but no teardown.

You can also define set-up methods across inheritance boundaries, in both base classes and derived classes. They will work together as if they were all defined in the same class.

Advanced Test Attributes

Use the [Ignore] attribute when you don't want the testing framework to run the specified tests.

[Test] [Ignore("Out of time. Will Complete on Monday. -- JJ" )] public void Something() { .. }

NUnit will report that this method was skipped and show a yellow bar in the GUI version.

Use the [Platform] attribute to ignore tests because of the platform on which you are running.

[Test] [Platform(Exclude = "Mono" )] public void RemoveOnEmpty() { .. } [Test, Platform(Exclude = "Net-1.0,Win95" )] public void EmptyStatusBar() { .. }

When using the [Platform] attribute, you will still get a green bar in the GUI even in the presence of tests ignored via this attribute. A comprehensive list of the platforms can be found in the NUnit documentation on

To test for a specific exception use the [ExpectedException] attribute.

[TestFixture] public class ImportListTests { [Test] [ExpectedException(typeof(ArgumentNullException))] public void NullList() { WhitePages.ImportList(null); // Shouldn't get to here } }

Once the expected exception fires, any remaining code in the test method will be skipped.

Test Categories

You can use NUnit categories to help sort out tests that run constantly versus long-running tests that should only run during the automated build. Categories are generally used for exclusion rather than inclusion.

A category is specified as an attribute. You provide a string to identify the category when you declare the method. Then when you run the tests, you can specify which categories you want to run (you can specify more than one).

[Test] [Category("Short" )] public void Use5Cities() { Assert.That(tsp.ShortestPath(5), Is.AtMost(140)); } // This one takes a while... [Test] [Category("Long" )] [Category("Jeffrey" )] public void Use50Cities() { Assert.That(tsp.ShortestPath(50), Is.AtMost(2300)); }

You can also mark entire fixtures as belonging to a category. And not all tests need categories.

You can specify the Explicit attribute: [Explicit("SpecialEquipmentNeeded" )] to run only when explicitly selected. This syntax automatically excludes the category from a run that doesn't specify any categories.

Each test should run independently of every other test; this allows you to run any individual test at any time, in any order.

NUnit Asserts

Asserts are the fundamental building block for unit tests and NUnit provides a number of different forms of assert as static methods in the Assert class.

Classic-style assertions

Constraint-style assertions

Comparison Constraints

Condition Constraints

Type Constraints

SameAs Constraints

String Constraints

Collection Constraints

File Assert