This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

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

CollectionAssert Class

As is true of developing production code, it is a good idea to make your test code as readable as possible. Although it is possible to create every unit test assertion using Assert.IsTrue, it is far better to choose an assertion method that more clearly expresses the intent of the test. If you are using the NUnit framework there are many such methods from which to choose, each with a different purpose.

If your assertions relate to sequences of values, collections or arrays, you should consider the methods provided by the CollectionAssert class. The contained assertions are designed to operate with any type that implements the IEnumerable interface. They allow you to compare entire or partial collections, check if items are present in a sequence and ensure arrays contain only unique items, objects of a specific type or are free of null references.

Comparing Entire Collections

A common requirement in a test is checking that two collections contain the same values. With CollectionAssert you can ensure that two sequences are identical, or that they contain the same values but not necessarily in the same order.

AreEqual and AreNotEqual

The AreEqual method compares the contents of two sequences and passes only if they are identical. The items in the two collections must be the same and must appear in the same order. In the case of reference types this usually means that the individual references must be the same. For value types the values must match. The two collections do not need to be of the same type, so a List and an array with the same values will pass the test.

The following example tests a method that calculates the squares of an input sequence. Collection.AreEqual allows us to confirm that all of the squares are correctly calculated and are returned in the expected order.

public void TheProcessedValuesAreTheSquaresOfTheInputSequence()
    int[] values = new int[] { 1, 3, 5, 9 };

    IEnumerable<int> processed = ProcessInput(values);

    int[] expected = new int[] { 1, 9, 25, 81 };
    CollectionAssert.AreEqual(expected, processed);

Sometimes an exact match is not the desired outcome. In these cases you might decide to add a comparer to the assertion. The comparer will generate a value from each pair of values. If the processed value from each collection matches in all cases, and in the correct order, the test passes. If there is a mismatch the test fails.

The following test exercising a method that converts a sequence of integers into English words. The comparer means that the case of the returned values is unimportant.

public void ASequenceOfIntegersCanBeConvertedToWords()
    int[] values = new int[] { 1, 2, 3, 4 };

    IEnumerable<string> words = ToWords(values);

    string[] expected = new string[] { "One", "two", "THREE", "four" };
    CollectionAssert.AreEqual(expected, words, StringComparer.CurrentCultureIgnoreCase);

There is a similar method named, "AreNotEqual", which performs the opposite function. The assertion fails if the two collections are identical.

AreEquivalent and AreNotEquivalent

AreEquivalent is similar in operation to AreEqual. The syntax for the two methods is the same but AreEquivalent is less strict. For the test to pass, the two sequences must contain the same values. However, the order of the items is not important.

The code below tests a method that sorts integer values. This test checks that the contents of the original collection are the same as the results, even though they will likely have been re-ordered. A second test would be required to ensure that the results are, in fact, ordered.

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

    IEnumerable<int> sorted = Sort(values);

    CollectionAssert.AreEquivalent(values, sorted);

Again, there is an opposing assertion that fails if the two collections contain the same values. This method is AreNotEquivalent.

Comparing Partial Collections

12 December 2013