Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value |
|---|---|
| Rule ID | MSTEST0068 |
| Title | Use Assert instead of CollectionAssert |
| Category | Usage |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default | Yes |
| Default severity | Info |
| Introduced in version | 4.3.0 |
| Is there a code fix | Yes |
Note
This rule is available starting with MSTest 4.3.
Cause
A test method uses CollectionAssert methods instead of equivalent Assert methods.
Rule description
Most CollectionAssert methods have equivalent counterparts in Assert that provide the same functionality. Prefer the Assert methods for consistency, better readability, improved discoverability, and alignment with behavior across all test frameworks.
The following CollectionAssert methods have equivalent Assert methods:
CollectionAssert.AreEqual(a, b)→Assert.AreSequenceEqual(a, b)CollectionAssert.AreNotEqual(a, b)→Assert.AreNotSequenceEqual(a, b)CollectionAssert.AreEquivalent(a, b)→Assert.AreSequenceEqual(a, b, SequenceOrder.InAnyOrder)CollectionAssert.AreNotEquivalent(a, b)→Assert.AreNotSequenceEqual(a, b, SequenceOrder.InAnyOrder)CollectionAssert.AllItemsAreNotNull(a)→Assert.AreAllNotNull(a)CollectionAssert.AllItemsAreUnique(a)→Assert.AreAllDistinct(a)CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(T))→Assert.AreAllOfType<T>(collection)CollectionAssert.Contains(collection, value)→Assert.Contains(value, collection)CollectionAssert.DoesNotContain(collection, value)→Assert.DoesNotContain(value, collection)
Warning
When migrating from CollectionAssert to Assert, be careful about the change in parameter order for Contains, DoesNotContain, and AllItemsAreInstancesOfType. In Assert, the expected value is always the first parameter.
Overloads of AreEqual/AreNotEqual that take an IComparer are skipped because Assert.AreSequenceEqual expects an IEqualityComparer<T> (different semantics). Overloads of AreEquivalent/AreNotEquivalent that take an IEqualityComparer<T> are also skipped. IsSubsetOf/IsNotSubsetOf have no direct Assert equivalent today and are not handled.
How to fix violations
Use the provided code fixer to automatically replace CollectionAssert method calls with their equivalent Assert methods. You can also manually replace the method calls if needed.
When to suppress warnings
Don't suppress warnings from this rule. The Assert methods provide the same functionality with better consistency.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable MSTEST0068
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0068
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.MSTEST0068.severity = none
For more information, see How to suppress code analysis warnings.