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.
- Fundamentals of Unit Testing: Getting Started With Unit Testing
- Fundamentals of Unit Testing: Test Your Application by Visual Studio Unit Test
- Fundamentals of Unit Testing: Understand AAA in Unit Testing
- Fundamental of Unit Testing: Understand AreEqual and AreEqual<T> in Unit Testing
- Fundamental of Unit Testing: Test Initialize and Test Setup
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.

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.

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







Gowtham RajamanickamPosted Apr 25, 2015, 5:29 AM
good