Edit

Share via


MSTEST0046: Use Assert instead of StringAssert

Property Value
Rule ID MSTEST0046
Title Use Assert instead of StringAssert
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default Yes
Default severity Info
Introduced in version 3.10.0
Is there a code fix Yes

Cause

A test method uses StringAssert methods instead of equivalent Assert methods.

Rule description

StringAssert methods have equivalent counterparts in Assert that provide the same functionality. Use the Assert methods for consistency, better readability, improved discoverability, and alignment with behavior across all test frameworks.

The following StringAssert methods have equivalent Assert methods:

  • StringAssert.Contains(value, substring)Assert.Contains(substring, value)
  • StringAssert.StartsWith(value, substring)Assert.StartsWith(substring, value)
  • StringAssert.EndsWith(value, substring)Assert.EndsWith(substring, value)
  • StringAssert.Matches(value, pattern)Assert.MatchesRegex(pattern, value)
  • StringAssert.DoesNotMatch(value, pattern)Assert.DoesNotMatchRegex(pattern, value)

Warning

When migrating from StringAssert to Assert methods, be careful about the change in parameter order. In Assert methods, the expected value is always the first parameter.

How to fix violations

Use the provided code fixer to automatically replace StringAssert 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.