Fundamentals of Unit Testing: Understand CollectionAssert() in Unit Testing

In this article we will talk about one important function of unit testing called CollectionAssert().

You are in the "Fundamentals of unit testing" article series. Here we are talking about unit testing using a Visual Studio unit test application. In our previous article we saw how to implement a simple unit test for small applications. You can read them here.

In this article we will talk about one important function of unit testing called CollectionAssert(). This function is very important for testing such a function that will throw a collection as return data.

Let's see example one by one.

AreEqual() function to match equality

This function can match two collections. If all items are the same in both collections then the test will pass otherwise fail.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  4. using TestProjectLibrary;  
  5. namespace UnitTest  
  6. {  
  7.     [TestClass]  
  8.     public class UnitTest1  
  9.     {  
  10.         [TestMethod]  
  11.         public void TestMethod1()  
  12.         {  
  13.             List<string> first = new List<string>();  
  14.             first.Add("a");  
  15.             List<string> second  = new List<string>();  
  16.             second.Add("b");  
  17.             CollectionAssert.AreEqual(first, second);  
  18.         }  
  19.     }  
  20. }  
Here the first collection contains “a” where the second is only “b”, so they are not equal. This is the test result.
 
test result

Now, we will change the collection content and now both collections has the same contents. Have a look at the following code.
  1. [TestMethod]  
  2. public void TestMethod1()  
  3. {  
  4.            List<string> first = new List<string>();  
  5.             first.Add("a");  
  6.    
  7.            List<string> second  = new List<string>();  
  8.             second.Add("a");  
  9.             CollectionAssert.AreEqual(first, second);  
  10. }  

We are seeing that the test has passed.

test

AllItemsAreUnique() to check whether all items are unique or not

In this example we are setting all new items in a collection. So the test should pass.
  1. [TestMethod]  
  2.     public void TestMethod1()  
  3.     {  
  4.         List<string> first = new List<string>();  
  5.         first.Add("a");  
  6.         first.Add("b");  
  7.         first.Add("c");  
  8.         CollectionAssert.AllItemsAreUnique(first);  
  9.     }  
  10. }  
Yes, the result agrees with us. Haha..
 
result

Contains() function to check whether collection contains an item

In this example we have added a, b and c in a collection but in the Contains() function we are checking whether or not “x” is present. Obviously it's not present.
  1. [TestMethod]  
  2. public void TestMethod1()  
  3. {  
  4.     List<string> first = new List<string>();  
  5.     first.Add("a");  
  6.     first.Add("b");  
  7.     first.Add("c");  
  8.     CollectionAssert.Contains(first, "x");  
  9. }  
And that's why the test fails.
 
Contains function

DoesNotContain() function

It's just the opposite to the DoesContain() function. If the item does not contain the test will pass. Here is a sample example.
  1. [TestMethod]  
  2. public void TestMethod1()  
  3. {  
  4.      List<string> first = new List<string>();  
  5.      first.Add("a");  
  6.      first.Add("b");  
  7.      first.Add("c");  
  8.      CollectionAssert.DoesNotContain(first, "x");  
  9. }  
And here is sample output.
 
sample output

ReferenceEquals() function to check reference

This function will ensure whether both arguments are referencing the same class. Have a look at the following example. Please keep in mind, it will check a reference type but not contain.
  1. [TestMethod]  
  2. public void TestMethod1()  
  3. {  
  4.      List<string> str1 = new List<string>();  
  5.      List<string> str2 = new List<string>();  
  6.      CollectionAssert.ReferenceEquals (str1,str2);  
  7. }  
The Test has passed because both are a collection of strings.
 
Collection of string

AllItemsAreNotNull() function

It will ensure that there is no null value in the collection. In the following code we have stored one null value in the collection.
  1. [TestMethod]  
  2. public void TestMethod1()  
  3. {   
  4.       List<string> str1 = new List<string>();  
  5.       str1.Add(null);  
  6.       str1.Add("a");  
  7.       str1.Add("b");  
  8.       CollectionAssert.AllItemsAreNotNull(str1);  
  9. }  

And that's why the test failed.

test fail

AllItemsAreInstanceOfType() function to

It will check whether or not all the items are instances of a specific type. Here is a sample code example.

  1. [TestMethod]  
  2. public void TestMethod1()  
  3. {  
  4.      List<string> str1 = new List<string>();  
  5.      CollectionAssert.AllItemsAreInstancesOfType(str1, typeof(string));  
  6. }  

 

The Test passed because all items are an instance of a string.

instance of string

Conclusion

I hope those functions will help you at the time of unit testing when you deal with a collection. In our next presentation we will discuss a few more topics of unit testing.