Legacy Documentation. View NUnit 3 Documentation

Assertions

Assertions are central to unit testing in any of the xUnit frameworks, and NUnit is no exception. NUnit provides a rich set of assertions as static methods of the Assert class.

If an assertion fails, the method call does not return and an error is reported. If a test contains multiple assertions, any that follow the one that failed will not be executed. For this reason, it's usually best to try for one assertion per test.

Each method may be called without a message, with a simple text message or with a message and arguments. In the last case the message is formatted using the provided text and arguments.

Two Models

Before NUnit 2.4, a separate method of the Assert class was used for each different assertion. We call this the classic model. It continues to be supported in NUnit, since many people prefer it.

Beginning with NUnit 2.4, a new constraint-based model is being introduced. This approach uses a single method of the Assert class for all assertions, passing a constraint object that specifies the test to be performed.

This constraint-based model is now used internally by NUnit for all assertions. The methods of the classic approach have been re-implemented on top of this new model.