As a C#/.NET developer, have you ever tried to write unit tests for legacy code that uses ArrayList
, Stack
, HashTable
or Queue
only to find that MSTest's assertion methods don't accept these non-generic collections? This frustrating limitation has been a pain point for many developers working with older codebases or third-party APIs.
Good news! I recently contributed to Microsoft Unit testing Framework and this open-source contribution has introduced comprehensive support for non-generic collections across multiple assertion methods. In this article, I'll walk you through the problem, the solution, and practical examples of how you can use these new features in your unit tests.
The Problem with Non-Generic Collections
MSTest assertion methods like Assert.Contains
only worked with generic IEnumerable<T>
collections. If you tried to use them with non-generic collections, you'd get compile-time errors.
[TestMethod]
public void TestMethod1()
{
ArrayList arrayList = new ArrayList {1, 2, 3, 4, "item5"};
var expected = 2;
// This won't compile!
Assert.Contains(expected, arrayList);
}
The Solution: New Overloads for Non-Generic Collections
The enhancement introduces overloads that accept IEnumerable (non-generic) for these assertion methods:
Assert.Contains
Assert.DoesNotContain
Assert.HasCount
Assert.IsEmpty
Assert.IsNotEmpty
Additionally, predicate-based overloads were added alongside non-generic collections for Contains and DoesNotContain, enabling powerful filtering logic.
Use Cases
From MSTEST.TestFramework version 4.0.0 and above, my open-source contributions have been incorporated into the framework, as seen below
Assert.Contains: Using the ArrayList Collection as a case study
Before (Compile time error)
![Screenshot 2025-10-07 205518]()
After
![Screenshot 2025-10-07 210307]()
Assert.DoesNotContain
Before
![Screenshot 2025-10-07 210654]()
After
![Screenshot 2025-10-07 210801]()
Assert.HasCount: Using Hashtable collection as a case study
Before
![Screenshot 2025-10-07 211211]()
After
![Screenshot 2025-10-07 211224]()
The same scenarios or case studies apply to Assert.IsEmpty, Assert.IsNotEmpty and Assert.HasCount, as they now accept non-generic collections overloads.
Conclusion
The introduction of non-generic collection support in MSTest's assertion methods is a significant quality-of-life improvement for developers working with legacy code. The predicate-based overloads are particularly powerful, enabling expressive and readable tests that were previously cumbersome to write.
Whether you're maintaining legacy systems, working with third-party APIs, or gradually migrating to modern collection types, these new assertion methods make your testing workflow smoother and more intuitive.
Key Takeaways
✅ Assert.Contains
, Assert.DoesNotContain
, Assert.HasCount
, Assert.IsEmpty
, and Assert.IsNotEmpty
now work with non-generic collections
✅ Predicate-based and non-generic collection overloads for Assert.Contains
and Assert.DoesNotContain
enable complex filtering logic in assertions
✅ Perfect for testing legacy code, COM objects, and third-party APIs
✅ Improves test readability and maintainability
✅ Reduces boilerplate code in unit tests