BlackWaspTM
Testing
.NET 1.1+

NUnit Condition Assertions

The eighth part of the Automated Unit Testing tutorial continues the description of the assertion commands provided by the NUnit framework. This article describes the condition assertions, which perform specific true or false tests.

Condition Asserts

The NUnit framework provides a large number of assertion commands that can be used in automated tests to ensure that the results of operations are as expected. In earlier articles we have seen assert methods that allow comparison of values and object references. In this article we will look at the condition asserts. These provide simple checks for Boolean values, null values, complex numbers or empty collections.

NB: In this article we will create several example tests that use nullable types. These are not available in the .NET framework version 1.1. However, the NUnit assert methods are compatible with early versions of the framework.

Assert.IsTrue

The IsTrue method asserts that the Boolean value provided to its parameter is true. In its most basic form you can test a Boolean variable. This is the case in the following example, where an exclusive OR operation is being tested:

public class Logic
{
    public bool? Xor(bool? a, bool? b)
    {
        return a ^ b;
    }
}

[TestFixture]
public class LogicTests
{
    [Test]
    public void TrueXorFalseEqualsTrue()
    {
        Logic logic = new Logic();
        bool? result = logic.Xor(true, false);
        Assert.IsTrue(result.Value);
    }
}

You can also use the IsTrue assert to ensure that any predicate evaluates to true. This allows you to create tests containing a single assert statement and any standard condition, where otherwise you may need multiple assertions. The following assertion contains a predicate, albeit a simple one:

Assert.IsTrue(result != null);

Assert.IsFalse

The IsFalse assertion is the opposite of IsTrue. Tests containing this method will pass only if the value is false:

[Test]
public void TrueXorTrueEqualsFalse()
{
    Logic logic = new Logic();
    bool? result = logic.Xor(true, true);
    Assert.IsFalse(result.Value);
}

Assert.IsNull

The next condition assert is IsNull. This assertion is used to ensure that a value is null. The following example uses IsNull to specify that an exclusive OR where one operand is null should return null.

[Test]
public void TrueXorNullEqualsNull()
{
    Logic logic = new Logic();
    bool? result = logic.Xor(true, null);
    Assert.IsNull(result);
}

Assert.IsNotNull

The reverse of IsNull is IsNotNull. This assertion passes for any value except null:

[Test]
public void XorOfTwoBooleansIsNotIsNull()
{
    Logic logic = new Logic();
    bool? result = logic.Xor(false, false);
    Assert.IsNotNull(result);
}

Assert.IsNaN

The fifth condition assert is IsNaN. It checks that a value is not a simple number. The assert passes if the value is a complex number or the result of dividing zero by zero. The parameter only accepts double-precision floating point numbers, though other types that can store NaN can be cast to double if necessary.

The sample below tests that obtaining the square root of a negative number returns NaN.

public class Mathematics
{
    public double SquareRoot(double square)
    {
        return Math.Sqrt(square);
    }
}

[TestFixture]
public class MathematicsTests
{
    [Test]
    public void SquareRootOfNegativeReturnsNaN()
    {
        Mathematics maths = new Mathematics();
        double result = maths.SquareRoot(-1);
        Assert.IsNaN(result);
    }
}
15 April 2011