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 ...

Get NUnit Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.