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

Assert.IsEmpty

The IsEmpty method can be used with any collection that implements the ICollection interface or with strings. The method asserts that the collection contains no items, or that a string is zero characters in length. In the example below, the method under test generates an ArrayList containing a specified number of strings. The test ensures that passing zero to the method results in an empty ArrayList.

using System.Collections;

public class ArrayListGenerator
{
    public ArrayList GetDotArrayList(int dots)
    {
        return ArrayList.Repeat(".", dots);
    }
}

[TestFixture]
public class ArrayListGeneratorTests
{
    [Test]
    public void ZeroLengthListsAreEmpty()
    {
        ArrayListGenerator generator = new ArrayListGenerator();
        ArrayList result = generator.GetDotArrayList(0);
        Assert.IsEmpty(result);
    }
}

Assert.IsNotEmpty

As with many of the NUnit assertions, IsEmpty has an opposite method. IsNotEmpty ensures that a collection contains at least one item or that a string includes one or more characters:

[Test]
public void PositiveLengthListsAreNotEmpty()
{
    ArrayListGenerator generator = new ArrayListGenerator();
    ArrayList result = generator.GetDotArrayList(5);
    Assert.IsNotEmpty(result);
}
15 April 2011