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 Collection Assertions

The basic assertions provided by the NUnit framework can be used for all unit tests. To make unit tests more readable, there are other assertion types that more clearly express the intent of the tests. This article describes the collection assertions.

Sometimes you will want to compare only parts of a collection, checking that one sequence contains another, usually shorter one. You might also want to test for the existence of a single value. CollectionAssert supports both possibilities.

IsSubsetOf and IsNotSubsetOf

As the name suggests, IsSubsetOf passes if all of the items in one collection are also present in a second. As with AreEquivalent, the items must be perfect matches but the order is unimportant. When using the method the smaller collection should be passed to the first parameter. The second argument is the sequence that will be searched.

The following example test checks that when the active contacts are retrieved from a database, they are added to the cache. The cache would be used to improve performance.

[Test]
public void RetrievingTheActiveContactsUsesTheCache()
{
    IList<Person> cache = new List<Person>
    {
        new Person { Name = "Bob" },
        new Person { Name = "Sue" },
        new Person { Name = "Jim" }
    };

    IEnumerable<Person> active = RetrieveActiveContacts(cache);

    CollectionAssert.IsSubsetOf(active, cache);
}

If you want to ensure that the items of one collection are not all present within a second, you can use CollectionAssert.IsNotSubsetOf.

Contains and DoesNotContain

If you only need to ensure that a single item is present within a collection, use the Contains assertion. This passes if the item exists and fails otherwise. The sample below tests that when a single contact is retrieved, it is cached.

[Test]
public void RetrievingAnUncachedContactAddsThePersonToTheCache()
{
    IList<Person> cache = new List<Person>();

    Person active = RetrieveContact("Bob", cache);

    CollectionAssert.Contains(cache, _bob);
}

Checking All Items in a Collection

There are several assertions that you can use to examine all of the items within a single collection. You can use these to check that the items are correctly ordered, that no null references are held, that all items are of a particular type, or that a collection only includes unique items.

IsOrdered

The IsOrdered assertion passes if all of the items in a collection are sorted correctly. If you only provide the collection to the method, the test passes if the items are ordered according to the default comparer for their type. For other sort orders you can supply a comparer as the second argument.

Earlier you saw a test that checked that the results of a Sort method included all of the original items. The companion for that test would check the order. This is shown below:

[Test]
public void TheSortedValuesAreInAscendingOrder()
{
    int[] values = new int[] { 1, 5, 4, 23, 8, 2 };

    IEnumerable<int> sorted = Sort(values);

    CollectionAssert.IsOrdered(sorted);
}

The next example checks that a sequence of Person objects is ordered by name. You can assume that the PersonNameComparer correctly compares two Person objects by extracting their Name property values.

[Test]
public void TheActiveContactsAreInAscendingOrderOfName()
{
    IList<Person> cache = new List<Person>
    {
        new Person { Name = "Bob" },
        new Person { Name = "Sue" },
        new Person { Name = "Jim" }
    };

    IEnumerable<Person> active = RetrieveActiveContacts(cache);

    CollectionAssert.IsOrdered(active, new PersonNameComparer());
}
12 December 2013