|
| 1 | +--- |
| 2 | +title: "MSTEST0046: Use Assert instead of StringAssert" |
| 3 | +description: "Learn about code analysis rule MSTEST0046: Use Assert instead of StringAssert" |
| 4 | +ms.date: 07/24/2025 |
| 5 | +f1_keywords: |
| 6 | +- MSTEST0046 |
| 7 | +- StringAssertToAssertAnalyzer |
| 8 | +helpviewer_keywords: |
| 9 | +- StringAssertToAssertAnalyzer |
| 10 | +- MSTEST0046 |
| 11 | +author: Evangelink |
| 12 | +ms.author: amauryleve |
| 13 | +--- |
| 14 | +# MSTEST0046: Use Assert instead of StringAssert |
| 15 | + |
| 16 | +| Property | Value | |
| 17 | +|-------------------------------------|------------------------------------------------------------------------------------------| |
| 18 | +| **Rule ID** | MSTEST0046 | |
| 19 | +| **Title** | Use Assert instead of StringAssert | |
| 20 | +| **Category** | Usage | |
| 21 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 22 | +| **Enabled by default** | Yes | |
| 23 | +| **Default severity** | Info | |
| 24 | +| **Introduced in version** | 3.10.0 | |
| 25 | +| **Is there a code fix** | Yes | |
| 26 | + |
| 27 | +## Cause |
| 28 | + |
| 29 | +A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods instead of equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods. |
| 30 | + |
| 31 | +## Rule description |
| 32 | + |
| 33 | +<xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods have equivalent counterparts in <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> that provide the same functionality. Use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods for consistency, better readability, improved discoverability, and alignment with behavior across all test frameworks. |
| 34 | + |
| 35 | +The following <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> methods have equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods: |
| 36 | + |
| 37 | +- `StringAssert.Contains(value, substring)` → `Assert.Contains(substring, value)` |
| 38 | +- `StringAssert.StartsWith(value, substring)` → `Assert.StartsWith(substring, value)` |
| 39 | +- `StringAssert.EndsWith(value, substring)` → `Assert.EndsWith(substring, value)` |
| 40 | +- `StringAssert.Matches(value, pattern)` → `Assert.MatchesRegex(pattern, value)` |
| 41 | +- `StringAssert.DoesNotMatch(value, pattern)` → `Assert.DoesNotMatchRegex(pattern, value)` |
| 42 | + |
| 43 | +> [!WARNING] |
| 44 | +> When migrating from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> to <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods, be careful about the change in parameter order. In <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods, the expected value is always the first parameter. |
| 45 | +
|
| 46 | +## How to fix violations |
| 47 | + |
| 48 | +Use the provided code fixer to automatically replace <xref:Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert> method calls with their equivalent <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods. You can also manually replace the method calls if needed. |
| 49 | + |
| 50 | +## When to suppress warnings |
| 51 | + |
| 52 | +Don't suppress warnings from this rule. The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods provide the same functionality with better consistency. |
0 commit comments