NUnit
|    
* *"
    |

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.

For convenience of presentation, we list the available assertions in three groups: comparisons, single condition tests and the Assert.Fail method.

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( decimal expected, decimal actual );
Assert.AreEqual( decimal expected, decimal actual, string message );

Assert.AreEqual( float expected, float actual, float tolerance );
Assert.AreEqual( float expected, float actual, float tolerance,
                 string message );

Assert.AreEqual( double expected, double actual, double tolerance );
Assert.AreEqual( double expected, double actual, double tolerance,
                 string message );

Assert.AreEqual( object expected, object actual );
Assert.AreEqual( object expected, object actual, string message );

Assert.AreSame( object expected, object actual );
Assert.AreSame( object expected, object actual, string message );

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.IsFalse( bool condition);
Assert.IsFalse( bool condition, string message );

Assert.IsNull( object anObject );
Assert.IsNull( object anObject, string message );

Assert.IsNotNull( object anObject );
Assert.IsNotNull( object anObject, string message );

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

Assert.Fail();
Assert.Fail( string message );

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 );
}


Copyright © 2004 Charlie Poole. All Rights Reserved.
|