Unit testing with CollectionAssert

Anyone using the unit testing framework in VSTS has surely become familiar with the Assert class. The Assert class has tonnes of methods to compare results but usually I find they are only good for scalar values and/or individual objects. When we start dealing with collections then one has to write extra code that usually entails looping through the collection and comparing results as you go. And let's face it, we all deal with arrays and collections on a regular basis.

Enter CollectionAssert.

CollectionAssert enables you to perform typical tests on arrays and collections with so much ease that unit testing fuctionality involving these data structures becomes so much more ... fun and productive. E.g.

'''<summary>

'''A test for CollectionAssert

'''</summary>

<TestMethod()> _

Public Sub CollectionAssertTest()

Dim array() As Integer = {1, 2, 3, 6, 6, 3, 6, 11}

Dim expected() As Integer = array

CollectionAssert.AreEqual(expected, array, "This could not have happened.") ' Pass

' CollectionAssert.AreNotEqual(expected, array) ' Fail

Dim MyCollection As New Collection()

For index As Integer = 1 To 10

MyCollection.Add("Value" & 1)

Next

CollectionAssert.AllItemsAreNotNull(MyCollection) ' Pass

CollectionAssert.AllItemsAreUnique(MyCollection) ' Pass

End Sub