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.
For convenience of presentation, we list the available
assertions in three groups: comparisons,
single condition tests
and utility methods.
Comparisons
Assertions that perform comparisons are often your best choice
because they report both expected and actual values. The expected
value is always the first argument. NUnit provides the following
comparison asserts:
Assert.AreEqual( int expected, int actual );
Assert.AreEqual( int expected, int actual, string message );
Assert.AreEqual( int expected, int actual, string message,
object[] parms );
Assert.AreEqual( decimal expected, decimal actual );
Assert.AreEqual( decimal expected, decimal actual, string message );
Assert.AreEqual( decimal expected, decimal actual, string message,
object[] parms );
Assert.AreEqual( float expected, float actual, float tolerance );
Assert.AreEqual( float expected, float actual, float tolerance,
string message );
Assert.AreEqual( float expected, float actual, float tolerance,
string message, object[] parms );
Assert.AreEqual( double expected, double actual, double tolerance );
Assert.AreEqual( double expected, double actual, double tolerance,
string message );
Assert.AreEqual( double expected, double actual, double tolerance,
string message, object[] parms );
Assert.AreEqual( object expected, object actual );
Assert.AreEqual( object expected, object actual, string message );
Assert.AreEqual( object expected, object actual, string message,
object[] parms );
Assert.AreSame( object expected, object actual );
Assert.AreSame( object expected, object actual, string message );
Assert.AreSame( object expected, object actual, string message,
object[] parms );
|
The AreSame method tests that the same objects are
referenced by both arguments. All the variants of AreEqual
test for equality.
Overloaded AreEqual methods are provided
for the most common types. In addition, the overload that compares
two objects makes special provisions so that numeric values of
different types compare as expected. This makes it possible for
asserts like the following to succeed:
Assert.AreEqual( 5, 5.0 )
Starting with version 2.2, special provision is also made for comparing arrays.
Two arrays will be treated as equal by Assert.AreEqual
if they are the same length and each of the corresponding elements is equal.
Note: In version 2.2, this only applies to arrays, not to
other collection types, such as ArrayList.
Condition Tests
Methods that test a specific condition are named for the condition
they test and take the value tested as their first argument and,
optionally a message as the second. The following methods are provided:
Assert.IsTrue( bool condition );
Assert.IsTrue( bool condition, string message );
Assert.IsTrue( bool condition, string message, object[] parms );
Assert.IsFalse( bool condition);
Assert.IsFalse( bool condition, string message );
Assert.IsFalse( bool condition, string message, object[] parms );
Assert.IsNull( object anObject );
Assert.IsNull( object anObject, string message );
Assert.IsNull( object anObject, string message, object[] parms );
Assert.IsNotNull( object anObject );
Assert.IsNotNull( object anObject, string message );
Assert.IsNotNull( object anObject, string message, object[] parms );
|
Utility Methods
Two utility methods, Fail() and Ignore() are provided in order
to allow more direct control of the test process:
Assert.Fail();
Assert.Fail( string message );
Assert.Fail( string message, object[] parms );
Assert.Ignore();
Assert.Ignore( string message );
Assert.Ignore( string message, object[] parms );
|
The Assert.Fail method provides you with the ability to
generate a failure based on tests that are not encapsulated by the
other methods. It is also useful in developing your own project-specific
assertions.
Here's an example of its use to create a private assertion that
tests whether a string contains an expected value.
public void AssertStringContains( string expected, string actual )
{
AssertStringContains( expected, text, string.Empty );
}
public void AssertStringContains( string expected, string actual,
string message )
{
if ( actual.IsIndexOf( expected ) < 0 )
Assert.Fail( string message );
}
|
The Assert.Ignore method provides you with the ability to dynamically
cause a test or suite to be ignored at runtime. It may be called in a test, setup or
fixture setup method. We recommend that you use this only in isolated
cases. The category facility is provided for more extensive inclusion
or exclusion of tests or you may elect to simply divide tests run
on different occasions into different assemblies.
|