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

Introduction

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 examples one by one.

Example 1. Equal() Function

Are Equal() functions 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.

using System;  
using System.Collections.Generic;  
using Microsoft.VisualStudio.TestTools.UnitTesting;  
using TestProjectLibrary;  
namespace UnitTest  
{  
    [TestClass]  
    public class UnitTest1  
    {  
        [TestMethod]  
        public void TestMethod1()  
        {  
            List<string> first = new List<string>();  
            first.Add("a");  
            List<string> second  = new List<string>();  
            second.Add("b");  
            CollectionAssert.AreEqual(first, second);  
        }  
    }  
}  

Here the first collection contains “a” whereas whereas the second is only “b”, so they are not equal. This is the test result.

Failed test-1

Now, we will change the collection content, and now both collections have the same content Have a look at the following code

[TestMethod]  
public void TestMethod1()  
{  
           List<string> first = new List<string>();  
            first.Add("a");    
           List<string> second  = new List<string>();  
            second.Add("a");  
            CollectionAssert.AreEqual(first, second);  
}  

We are seeing that the test has passed.

Passes tests

Example 2. Unique() Function

All Items are Unique() 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

[TestMethod]  
    public void TestMethod1()  
    {  
        List<string> first = new List<string>();  
        first.Add("a");  
        first.Add("b");  
        first.Add("c");  
        CollectionAssert.AllItemsAreUnique(first);  
    }  
}  

Yes, the result agrees with us. Haha

Passes test

Example 3. Contains() Function

Contains() function to check whether the 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. It's not present

[TestMethod]  
public void TestMethod1()  
{  
    List<string> first = new List<string>();  
    first.Add("a");  
    first.Add("b");  
    first.Add("c");  
    CollectionAssert.Contains(first, "x");  
}  

And that's why the test fails.

Failed test

Example 4. Not Contain() Function

DoesNotContain() function

It's just the opposite of the DoesContain() function. If the item does not contain the test will pass. Here is a sample example

[TestMethod]  
public void TestMethod1()  
{  
     List<string> first = new List<string>();  
     first.Add("a");  
     first.Add("b");  
     first.Add("c");  
     CollectionAssert.DoesNotContain(first, "x");  
}  

Here is the sample output.

Passes test

Example 5. Reference Equals() Function

ReferenceEquals() function to check the reference

This function will ensure whether both arguments are referencing the same class. Have a look at the following example. Please keep in mind, that it will check a reference type but not contain

[TestMethod]  
public void TestMethod1()  
{  
     List<string> str1 = new List<string>();  
     List<string> str2 = new List<string>();  
     CollectionAssert.ReferenceEquals (str1,str2);  
}  

The Test has passed because both are a collection of strings.

Passes test

Example 6. Not Null() Function

All Items are not Null() 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.

[TestMethod]  
public void TestMethod1()  
{   
      List<string> str1 = new List<string>();  
      str1.Add(null);  
      str1.Add("a");  
      str1.Add("b");  
      CollectionAssert.AllItemsAreNotNull(str1);  
} 

And that's why the test failed.

Failed test

Example 7. Instances of Type() Function

All Items are Instances of type() functions to

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

[TestMethod]  
public void TestMethod1()  
{  
     List<string> str1 = new List<string>();  
     CollectionAssert.AllItemsAreInstancesOfType(str1, typeof(string));  
}  

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

Passes test

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.


Similar Articles