Search Results for

    Show / Hide Table of Contents

    CollectionAssert

    The CollectionAssert class provides a number of methods that are useful when examining collections and their contents or for comparing two collections. These methods may be used with any object implementing IEnumerable.

    The AreEqual overloads succeed if the corresponding elements of the two collections are equal. AreEquivalent tests whether the collection contents are equal, but without regard to order. In both cases, elements are compared using NUnit's default equality comparison.

    CollectionAssert is in the NUnit.Framework namespace, so using NUnit.Framework; is enough. It ships in nunit.framework.legacy.dll, which is part of the NUnit package.

    Note

    In NUnit 4, CollectionAssert was in the NUnit.Framework.Legacy namespace and needed using NUnit.Framework.Legacy;.

    CollectionAssert.AllItemsAreInstancesOfType(IEnumerable collection, Type expectedType);
    CollectionAssert.AllItemsAreInstancesOfType(
        IEnumerable collection, Type expectedType, string message, params object[] args);
    
    CollectionAssert.AllItemsAreNotNull(IEnumerable collection);
    CollectionAssert.AllItemsAreNotNull(
        IEnumerable collection, string message, params object[] args);
    
    CollectionAssert.AllItemsAreUnique(IEnumerable collection);
    CollectionAssert.AllItemsAreUnique(
        IEnumerable collection, string message, params object[] args);
    
    CollectionAssert.AreEqual(IEnumerable expected, IEnumerable actual);
    CollectionAssert.AreEqual(
        IEnumerable expected, IEnumerable actual, string message, params object[] args);
    
    CollectionAssert.AreEquivalent(IEnumerable expected, IEnumerable actual);
    CollectionAssert.AreEquivalent(
        IEnumerable expected, IEnumerable actual, string message, params object[] args);
    
    CollectionAssert.AreNotEqual(IEnumerable expected, IEnumerable actual);
    CollectionAssert.AreNotEqual(
        IEnumerable expected, IEnumerable actual, string message, params object[] args);
    
    CollectionAssert.AreNotEquivalent(IEnumerable expected, IEnumerable actual);
    CollectionAssert.AreNotEquivalent(
        IEnumerable expected, IEnumerable actual, string message, params object[] args);
    
    CollectionAssert.Contains(IEnumerable expected, object actual);
    CollectionAssert.Contains(
        IEnumerable expected, object actual, string message, params object[] args);
    
    CollectionAssert.DoesNotContain(IEnumerable expected, object actual);
    CollectionAssert.DoesNotContain(
        IEnumerable expected, object actual, string message, params object[] args);
    
    CollectionAssert.IsSubsetOf(IEnumerable subset, IEnumerable superset);
    CollectionAssert.IsSubsetOf(
        IEnumerable subset, IEnumerable superset, string message, params object[] args);
    
    CollectionAssert.IsNotSubsetOf(IEnumerable subset, IEnumerable superset);
    CollectionAssert.IsNotSubsetOf(
        IEnumerable subset, IEnumerable superset, string message, params object[] args);
    
    CollectionAssert.IsEmpty(IEnumerable collection);
    CollectionAssert.IsEmpty(
        IEnumerable collection, string message, params object[] args);
    
    CollectionAssert.IsNotEmpty(IEnumerable collection);
    CollectionAssert.IsNotEmpty(
        IEnumerable collection, string message, params object[] args);
    
    CollectionAssert.IsOrdered(IEnumerable collection);
    CollectionAssert.IsOrdered(
        IEnumerable collection, string message, params object[] args);
    
    CollectionAssert.IsOrdered(IEnumerable collection, IComparer comparer);
    CollectionAssert.IsOrdered(IEnumerable collection,
        IComparer comparer, string message, params object[] args);
    

    Examples

    Comparing collections

    [Test]
    public void CollectionAssert_Basic_Examples()
    {
        var list1 = new[] { 1, 2, 3 };
        var list2 = new[] { 1, 2, 3 };
        var list3 = new[] { 3, 2, 1 };
        var list4 = new[] { 1, 2, 4 };
        
        // Collections are equal (same order)
        CollectionAssert.AreEqual(list1, list2);
        
        // Collections are equivalent (same items, any order)
        CollectionAssert.AreEquivalent(list1, list3);
        
        // Collections are not equal
        CollectionAssert.AreNotEqual(list1, list4);
        
        // Collections are not equivalent
        CollectionAssert.AreNotEquivalent(list1, list4);
    }
    

    Testing for members

    [Test]
    public void CollectionAssert_Contains_Examples()
    {
        var list = new[] { 1, 2, 3, 4, 5 };
        
        CollectionAssert.Contains(list, 3);
        CollectionAssert.DoesNotContain(list, 10);
    }
    

    Subsets

    [Test]
    public void CollectionAssert_Subset_Examples()
    {
        var superset = new[] { 1, 2, 3, 4, 5 };
        var subset = new[] { 2, 4 };
        var notSubset = new[] { 1, 6 };
        
        CollectionAssert.IsSubsetOf(subset, superset);
        CollectionAssert.IsNotSubsetOf(notSubset, superset);
    }
    

    Empty collections

    [Test]
    public void CollectionAssert_Empty_Examples()
    {
        var emptyList = new int[] { };
        var nonEmptyList = new[] { 1, 2, 3 };
        
        CollectionAssert.IsEmpty(emptyList);
        CollectionAssert.IsNotEmpty(nonEmptyList);
    }
    

    Ordering

    [Test]
    public void CollectionAssert_Ordered_Examples()
    {
        var orderedList = new[] { 1, 2, 3, 4, 5 };
        var reverseOrderedList = new[] { 5, 4, 3, 2, 1 };
        
        CollectionAssert.IsOrdered(orderedList);
        CollectionAssert.IsOrdered(reverseOrderedList, Comparer<int>.Create((x, y) => y.CompareTo(x)));
    }
    

    Item types

    [Test]
    public void CollectionAssert_ItemType_Examples()
    {
        var stringList = new[] { "a", "b", "c" };
        var mixedList = new object[] { "string", 123, null };
        
        CollectionAssert.AllItemsAreInstancesOfType(stringList, typeof(string));
        CollectionAssert.AllItemsAreNotNull(stringList);
        CollectionAssert.AllItemsAreUnique(stringList);
    }
    

    See Also

    • Collection Constraints
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0