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+

NUnit Utility Assertions

The eleventh part of the Automated Unit Testing tutorial completes the examination of the NUnit Framework's assert methods. This instalment considers the utility asserts, which send instructions to the test runner, rather than performing tests.

Utility Asserts

In the last six instalments of the tutorial we have looked at six categories of NUnit assertion methods. Each of these groups of asserts can be used within tests to ensure that the results of an operation are correct. In this article we will look at the utility asserts. Unlike the previous items, these assert methods are not used to test the correctness of your software. Instead, they are used to send messages to the test runner that tell it to present a specific result.

The utility asserts are useful when the tests for an application are not yet complete, or where you are creating an outline of the tests that you wish to implement later. For example, you may wish to define the full set of tests by creating classes and methods to which you will later add test code. If your source control system rules specify that you cannot check in code if any test fails, you may decide to use a utility assert in each method that indicates success. If your source control rules are less stringent, you may decide to use the asserts that specify that the tests fail or that the results are inconclusive. You can also use asserts to ignore tests or fixtures completely.

Assert.Pass

The first utility assert is Assert.Pass. If this method is used, the test passes. If the call to the assert method is the only code within a test, the result is the same as if it were not present and will actually slow the performance of the test runner slightly. However, if you pass a string argument, you can associate a message with the test, perhaps explaining why it is not currently implemented.

[Test]
public void PassAssert()
{
    Assert.Pass("This functionality is not yet implemented");
}

NB: When using the graphical test runner, messages are displayed in the message area only when a test fails. To see the messages associated with a call to Assert.Pass, right-click the test in question and choose "Properties" from the menu that appears.

Assert.Fail

Assert.Fail is the opposite of Assert.Pass. A test containing this assertion will always fail.

[Test]
public void FailAssert()
{
    Assert.Fail("This functionality is not yet implemented");
}

Assert.Inconclusive

Assert.Inconclusive allows a third option for the result of a test. When used, the result of the test method is set to inconclusive. In the graphical test runner you see the test highlighted with a purple question mark (?).

[Test]
public void InconclusiveAssert()
{
    Assert.Inconclusive("This functionality is not yet implemented");
}

Assert.Ignore

The final utility assert is Assert.Ignore. As the name suggests, when the test runner encounters this assertion it ceases execution of the test and reports no result. To ignore a single test you can add the assert within the test method. To ignore a test fixture, add the call within the method decorated with the SetUp attribute.

In the sample below the method is ignored, even though the second assert would normally cause a failure.

[Test]
public void IgnoreAssert()
{
    Assert.Ignore();
    Assert.IsTrue(false);
}
25 April 2011