Skip to content

Commit 95053cf

Browse files
authored
Add missing analyzers docs (#50819)
1 parent fe2e868 commit 95053cf

File tree

10 files changed

+746
-1
lines changed

10 files changed

+746
-1
lines changed

docs/core/testing/mstest-analyzers/mstest0055.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ai-usage: ai-generated
2727

2828
## Cause
2929

30-
A call to `string.Contains`, `string.StartsWith`, or `string.EndsWith` is made and its return value is ignored.
30+
A call to <xref:System.String.Contains%2A>, <xref:System.String.StartsWith%2A>, or <xref:System.String.EndsWith%2A> is made and its return value is ignored.
3131

3232
## Rule description
3333

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: "MSTEST0056: Use DisplayName property for test method display names"
3+
description: "Learn about code analysis rule MSTEST0056: Use DisplayName property for test method display names"
4+
ms.date: 12/29/2025
5+
f1_keywords:
6+
- MSTEST0056
7+
- TestMethodAttributeShouldSetDisplayNameCorrectlyAnalyzer
8+
helpviewer_keywords:
9+
- TestMethodAttributeShouldSetDisplayNameCorrectlyAnalyzer
10+
- MSTEST0056
11+
author: evangelink
12+
ms.author: amauryleve
13+
ai-usage: ai-assisted
14+
dev_langs:
15+
- CSharp
16+
---
17+
# MSTEST0056: Use DisplayName property for test method display names
18+
19+
| Property | Value |
20+
|-------------------------------------|----------------------------------------------------|
21+
| **Rule ID** | MSTEST0056 |
22+
| **Title** | Use DisplayName property for test method display names |
23+
| **Category** | Usage |
24+
| **Fix is breaking or non-breaking** | Non-breaking |
25+
| **Enabled by default** | Yes |
26+
| **Default severity** | Warning |
27+
| **Introduced in version** | 4.0.0 |
28+
| **Is there a code fix** | Yes |
29+
30+
## Cause
31+
32+
A test method attribute uses a string constructor argument instead of the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.DisplayName> property.
33+
34+
## Rule description
35+
36+
When specifying a custom display name for a test method, you should use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.DisplayName> property instead of passing a string as a constructor argument. In MSTest 4.0 and later, the string constructor argument is used for source information (caller file path) rather than display name, which is a breaking change from MSTest 3.x. Using the `DisplayName` property ensures your code works correctly and avoids potential confusion.
37+
38+
```csharp
39+
[TestClass]
40+
public class TestClass
41+
{
42+
[TestMethod("My Test Name")] // Violation
43+
public void TestMethod()
44+
{
45+
// Test code
46+
}
47+
}
48+
```
49+
50+
## How to fix violations
51+
52+
Replace the constructor argument with the `DisplayName` property.
53+
54+
```csharp
55+
[TestClass]
56+
public class TestClass
57+
{
58+
[TestMethod(DisplayName = "My Test Name")]
59+
public void TestMethod()
60+
{
61+
// Test code
62+
}
63+
}
64+
```
65+
66+
## When to suppress warnings
67+
68+
Do not suppress warnings from this rule. Using the `DisplayName` property is the only way to specify custom display names for test methods. If your intent is to explicitly pass a value for the caller file path parameter instead of setting a display name, you can suppress this warning, but this is rarely needed.
69+
70+
## Suppress a warning
71+
72+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
73+
74+
```csharp
75+
#pragma warning disable MSTEST0056
76+
// The code that's violating the rule is on this line.
77+
#pragma warning restore MSTEST0056
78+
```
79+
80+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).
81+
82+
```ini
83+
[*.{cs,vb}]
84+
dotnet_diagnostic.MSTEST0056.severity = none
85+
```
86+
87+
For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: "MSTEST0057: Propagate source information in custom test method attributes"
3+
description: "Learn about code analysis rule MSTEST0057: Propagate source information in custom test method attributes"
4+
ms.date: 12/29/2025
5+
f1_keywords:
6+
- MSTEST0057
7+
- TestMethodAttributeShouldPropagateSourceInformationAnalyzer
8+
helpviewer_keywords:
9+
- TestMethodAttributeShouldPropagateSourceInformationAnalyzer
10+
- MSTEST0057
11+
author: evangelink
12+
ms.author: amauryleve
13+
ai-usage: ai-assisted
14+
dev_langs:
15+
- CSharp
16+
---
17+
# MSTEST0057: Propagate source information in custom test method attributes
18+
19+
| Property | Value |
20+
|-------------------------------------|----------------------------------------------------|
21+
| **Rule ID** | MSTEST0057 |
22+
| **Title** | Propagate source information in custom test method attributes |
23+
| **Category** | Usage |
24+
| **Fix is breaking or non-breaking** | Non-breaking |
25+
| **Enabled by default** | Yes |
26+
| **Default severity** | Warning |
27+
| **Introduced in version** | 4.0.0 |
28+
| **Is there a code fix** | Yes |
29+
30+
## Cause
31+
32+
A custom <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> class does not propagate caller information to the base class constructor.
33+
34+
## Rule description
35+
36+
When creating custom test method attributes that derive from <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>, you should propagate source information using caller information attributes. This allows MSTest to correctly track the source file and line number for test methods, improving diagnostics, test result reporting, and Test Explorer behavior.
37+
38+
```csharp
39+
public class MyTestMethodAttribute : TestMethodAttribute
40+
{
41+
public MyTestMethodAttribute() // Violation
42+
: base()
43+
{
44+
}
45+
}
46+
```
47+
48+
## How to fix violations
49+
50+
Add `CallerFilePath` and `CallerLineNumber` parameters to the constructor and pass them to the base class.
51+
52+
```csharp
53+
using System.Runtime.CompilerServices;
54+
55+
public class MyTestMethodAttribute : TestMethodAttribute
56+
{
57+
public MyTestMethodAttribute(
58+
[CallerFilePath] string callerFilePath = "",
59+
[CallerLineNumber] int callerLineNumber = -1)
60+
: base(callerFilePath, callerLineNumber)
61+
{
62+
}
63+
}
64+
```
65+
66+
## When to suppress warnings
67+
68+
Do not suppress warnings from this rule. Propagating source information is essential for proper test reporting and diagnostics.
69+
70+
## Suppress a warning
71+
72+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
73+
74+
```csharp
75+
#pragma warning disable MSTEST0057
76+
// The code that's violating the rule is on this line.
77+
#pragma warning restore MSTEST0057
78+
```
79+
80+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).
81+
82+
```ini
83+
[*.{cs,vb}]
84+
dotnet_diagnostic.MSTEST0057.severity = none
85+
```
86+
87+
For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: "MSTEST0058: Avoid assertions in catch blocks"
3+
description: "Learn about code analysis rule MSTEST0058: Avoid assertions in catch blocks"
4+
ms.date: 12/29/2025
5+
f1_keywords:
6+
- MSTEST0058
7+
- AvoidAssertsInCatchBlocksAnalyzer
8+
helpviewer_keywords:
9+
- AvoidAssertsInCatchBlocksAnalyzer
10+
- MSTEST0058
11+
author: evangelink
12+
ms.author: amauryleve
13+
ai-usage: ai-assisted
14+
dev_langs:
15+
- CSharp
16+
---
17+
# MSTEST0058: Avoid assertions in `catch` blocks
18+
19+
| Property | Value |
20+
|-------------------------------------|----------------------------------------------------|
21+
| **Rule ID** | MSTEST0058 |
22+
| **Title** | Avoid assertions in catch blocks |
23+
| **Category** | Usage |
24+
| **Fix is breaking or non-breaking** | Non-breaking |
25+
| **Enabled by default** | Yes |
26+
| **Default severity** | Info |
27+
| **Introduced in version** | 4.1.0 |
28+
| **Is there a code fix** | No |
29+
30+
## Cause
31+
32+
A test method contains assertion statements within a `catch` block.
33+
34+
## Rule description
35+
36+
Placing assertions in `catch` blocks is an anti-pattern that can lead to confusing test results and makes tests harder to understand. When an exception is thrown, the `catch` block executes, and the assertion runs. However, if no exception is thrown, the `catch` block never executes, potentially giving false confidence that the test passed.
37+
38+
Instead, use <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Throws*?displayProperty=nameWithType> or similar methods to verify that expected exceptions are thrown. This makes the test intent clearer and ensures proper test behavior.
39+
40+
```csharp
41+
[TestClass]
42+
public class TestClass
43+
{
44+
[TestMethod]
45+
public void TestMethod()
46+
{
47+
try
48+
{
49+
// Code that might throw.
50+
DoSomethingThatMightThrow();
51+
}
52+
catch (Exception ex)
53+
{
54+
Assert.AreEqual("Expected error message", ex.Message); // Violation
55+
}
56+
}
57+
}
58+
```
59+
60+
## How to fix violations
61+
62+
Use `Assert.Throws`, `Assert.ThrowsExactly` or related assertion methods to test for exceptions.
63+
64+
```csharp
65+
[TestClass]
66+
public class TestClass
67+
{
68+
[TestMethod]
69+
public void TestMethod_ThrowsExceptionWithExpectedMessage()
70+
{
71+
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => DoSomethingThatMightThrow());
72+
Assert.AreEqual("Expected error message", exception.Message);
73+
}
74+
}
75+
```
76+
77+
## When to suppress warnings
78+
79+
You might suppress this warning if you have a legitimate need to catch an exception and perform complex validation that cannot be easily expressed using standard assertion methods. However, consider refactoring your test to use more explicit exception assertions first.
80+
81+
## Suppress a warning
82+
83+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
84+
85+
```csharp
86+
#pragma warning disable MSTEST0058
87+
// The code that's violating the rule is on this line.
88+
#pragma warning restore MSTEST0058
89+
```
90+
91+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).
92+
93+
```ini
94+
[*.{cs,vb}]
95+
dotnet_diagnostic.MSTEST0058.severity = none
96+
```
97+
98+
For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
title: "MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes"
3+
description: "Learn about code analysis rule MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes"
4+
ms.date: 12/29/2025
5+
f1_keywords:
6+
- MSTEST0059
7+
- UseParallelizeAttributeAnalyzer
8+
helpviewer_keywords:
9+
- UseParallelizeAttributeAnalyzer
10+
- MSTEST0059
11+
author: evangelink
12+
ms.author: amauryleve
13+
ai-usage: ai-assisted
14+
dev_langs:
15+
- CSharp
16+
---
17+
# MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes
18+
19+
| Property | Value |
20+
|-------------------------------------|----------------------------------------------------|
21+
| **Rule ID** | MSTEST0059 |
22+
| **Title** | Do not use both Parallelize and DoNotParallelize attributes |
23+
| **Category** | Usage |
24+
| **Fix is breaking or non-breaking** | Non-breaking |
25+
| **Enabled by default** | Yes |
26+
| **Default severity** | Warning |
27+
| **Introduced in version** | 4.1.0 |
28+
| **Is there a code fix** | No |
29+
30+
## Cause
31+
32+
An assembly contains both <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute> attributes.
33+
34+
## Rule description
35+
36+
The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute> attributes are mutually exclusive at the assembly level. When both attributes are applied to the same assembly, tests run sequentially. This conflicting configuration indicates unclear intent and should be resolved by choosing one parallelization strategy for your test assembly.
37+
38+
```csharp
39+
using Microsoft.VisualStudio.TestTools.UnitTesting;
40+
41+
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] // Violation
42+
[assembly: DoNotParallelize]
43+
```
44+
45+
## How to fix violations
46+
47+
Remove one of the conflicting attributes based on your intended parallelization strategy.
48+
49+
If you want parallel execution:
50+
51+
```csharp
52+
using Microsoft.VisualStudio.TestTools.UnitTesting;
53+
54+
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
55+
```
56+
57+
If you want sequential execution:
58+
59+
```csharp
60+
using Microsoft.VisualStudio.TestTools.UnitTesting;
61+
62+
[assembly: DoNotParallelize]
63+
```
64+
65+
If you want to enable parallelization at the assembly level but disable it for specific classes or methods, apply `Parallelize` at the assembly level and `DoNotParallelize` at the class or method level:
66+
67+
```csharp
68+
using Microsoft.VisualStudio.TestTools.UnitTesting;
69+
70+
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
71+
72+
[DoNotParallelize]
73+
[TestClass]
74+
public class SequentialTests
75+
{
76+
[TestMethod]
77+
public void Test1() { }
78+
}
79+
80+
[TestClass]
81+
public class ParallelTests
82+
{
83+
[TestMethod]
84+
public void Test2() { }
85+
86+
[DoNotParallelize]
87+
[TestMethod]
88+
public void Test3() { }
89+
}
90+
```
91+
92+
## When to suppress warnings
93+
94+
Do not suppress warnings from this rule. Having both attributes creates ambiguous test configuration that should be resolved.
95+
96+
## Suppress a warning
97+
98+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
99+
100+
```csharp
101+
#pragma warning disable MSTEST0059
102+
// The code that's violating the rule is on this line.
103+
#pragma warning restore MSTEST0059
104+
```
105+
106+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../../../fundamentals/code-analysis/configuration-files.md).
107+
108+
```ini
109+
[*.{cs,vb}]
110+
dotnet_diagnostic.MSTEST0059.severity = none
111+
```
112+
113+
For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md).

0 commit comments

Comments
 (0)