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.
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” where the second is only “b”, so they are not equal. This is the test result.
Now, we will change the collection content and now both collections has the same contents. 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.
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.
[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..
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.
[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.
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.
[TestMethod]
public void TestMethod1()
{
List<string> first = new List<string>();
first.Add("a");
first.Add("b");
first.Add("c");
CollectionAssert.DoesNotContain(first, "x");
}
And here is 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.
[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.
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.
[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.
AllItemsAreInstanceOfType() function 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.
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.