O'Reilly logo

NUnit Pocket Reference by Bill Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unit Testing with NUnit

This section discusses the different elements of the NUnit framework and how to use them to create unit tests and test structures. Figure 1-2 shows the relationship between the NUnit elements.

In the simplest case, NUnit requires only test cases and one or more test fixtures. Each test must contain at least one assertion. A description of these elements follows.

NUnit test elements

Figure 1-2. NUnit test elements

Unit Testing with NUnit

Test cases

A test is the lowest building block of unit testing and tests a single piece of software functionality. Programmatically, a test corresponds to a method in the unit test code.

You identify a test by decorating a method with the [Test] attribute. For example:

[Test]
public void Test1()
{
    // test case implementation
}

A test method must be public (so that the test runner can locate it using reflection), returns void, and take no arguments.

A unit test performs one or more assertions that determine whether the functionality being tested works properly. An assertion simply tests an actual post-condition against the expected post-condition required for the test to pass. Assertions are described later in "Assertions."

The [Test] attribute has an optional argument named Description that defines the description that appears in the test properties dialog in the test runner GUI. For example:

[Test (Description = "MyTest")]

Warning

For backward compatibility ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required