-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add missing analyzers docs #50819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Evangelink
wants to merge
3
commits into
main
Choose a base branch
from
dev/amauryleve/mstest-analyzers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add missing analyzers docs #50819
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| title: "MSTEST0056: Use DisplayName property for test method display names" | ||
| description: "Learn about code analysis rule MSTEST0056: Use DisplayName property for test method display names" | ||
| ms.date: 12/29/2025 | ||
| f1_keywords: | ||
| - MSTEST0056 | ||
| - TestMethodAttributeShouldSetDisplayNameCorrectlyAnalyzer | ||
| helpviewer_keywords: | ||
| - TestMethodAttributeShouldSetDisplayNameCorrectlyAnalyzer | ||
| - MSTEST0056 | ||
| author: evangelink | ||
| ms.author: amauryleve | ||
| ai-usage: ai-assisted | ||
| dev_langs: | ||
| - CSharp | ||
| --- | ||
| # MSTEST0056: Use DisplayName property for test method display names | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|----------------------------------------------------| | ||
| | **Rule ID** | MSTEST0056 | | ||
| | **Title** | Use DisplayName property for test method display names | | ||
| | **Category** | Usage | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default** | Yes | | ||
| | **Default severity** | Warning | | ||
| | **Introduced in version** | 4.0.0 | | ||
| | **Is there a code fix** | Yes | | ||
|
|
||
| ## Cause | ||
|
|
||
| A test method attribute uses a string constructor argument instead of the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.DisplayName> property. | ||
|
|
||
| ## Rule description | ||
|
|
||
| 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. This ensures consistent usage across the MSTest framework and improves code readability. | ||
|
|
||
| ```csharp | ||
| [TestClass] | ||
| public class TestClass | ||
| { | ||
| [TestMethod("My Test Name")] // Violation | ||
| public void TestMethod() | ||
| { | ||
| // Test code | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| Replace the constructor argument with the `DisplayName` property. | ||
|
|
||
| ```csharp | ||
| [TestClass] | ||
| public class TestClass | ||
| { | ||
| [TestMethod(DisplayName = "My Test Name")] | ||
| public void TestMethod() | ||
| { | ||
| // Test code | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| Do not suppress warnings from this rule. Using the `DisplayName` property is the correct and recommended way to specify custom display names for test methods. | ||
|
|
||
| ## 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. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable MSTEST0056 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore MSTEST0056 | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ```ini | ||
| [*.{cs,vb}] | ||
| dotnet_diagnostic.MSTEST0056.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| title: "MSTEST0057: Propagate source information in custom test method attributes" | ||
| description: "Learn about code analysis rule MSTEST0057: Propagate source information in custom test method attributes" | ||
| ms.date: 12/29/2025 | ||
| f1_keywords: | ||
| - MSTEST0057 | ||
| - TestMethodAttributeShouldPropagateSourceInformationAnalyzer | ||
| helpviewer_keywords: | ||
| - TestMethodAttributeShouldPropagateSourceInformationAnalyzer | ||
| - MSTEST0057 | ||
| author: evangelink | ||
| ms.author: amauryleve | ||
| ai-usage: ai-assisted | ||
| dev_langs: | ||
| - CSharp | ||
| --- | ||
| # MSTEST0057: Propagate source information in custom test method attributes | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|----------------------------------------------------| | ||
| | **Rule ID** | MSTEST0057 | | ||
| | **Title** | Propagate source information in custom test method attributes | | ||
| | **Category** | Usage | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default** | Yes | | ||
| | **Default severity** | Warning | | ||
| | **Introduced in version** | 4.0.0 | | ||
| | **Is there a code fix** | Yes | | ||
|
|
||
| ## Cause | ||
|
|
||
| A custom <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> class does not propagate caller information to the base class constructor. | ||
|
|
||
| ## Rule description | ||
|
|
||
| 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 and test result reporting. | ||
|
|
||
| ```csharp | ||
| public class MyTestMethodAttribute : TestMethodAttribute | ||
| { | ||
| public MyTestMethodAttribute() // Violation | ||
| : base() | ||
| { | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| Add `CallerFilePath` and `CallerLineNumber` parameters to the constructor and pass them to the base class. | ||
|
|
||
| ```csharp | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| public class MyTestMethodAttribute : TestMethodAttribute | ||
| { | ||
| public MyTestMethodAttribute( | ||
| [CallerFilePath] string callerFilePath = "", | ||
| [CallerLineNumber] int callerLineNumber = -1) | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : base(callerFilePath, callerLineNumber) | ||
| { | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| Do not suppress warnings from this rule. Propagating source information is essential for proper test reporting and diagnostics. | ||
|
|
||
| ## 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. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable MSTEST0057 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore MSTEST0057 | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ```ini | ||
| [*.{cs,vb}] | ||
| dotnet_diagnostic.MSTEST0057.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| title: "MSTEST0058: Avoid assertions in catch blocks" | ||
| description: "Learn about code analysis rule MSTEST0058: Avoid assertions in catch blocks" | ||
| ms.date: 12/29/2025 | ||
| f1_keywords: | ||
| - MSTEST0058 | ||
| - AvoidAssertsInCatchBlocksAnalyzer | ||
| helpviewer_keywords: | ||
| - AvoidAssertsInCatchBlocksAnalyzer | ||
| - MSTEST0058 | ||
| author: evangelink | ||
| ms.author: amauryleve | ||
| ai-usage: ai-assisted | ||
| dev_langs: | ||
| - CSharp | ||
| --- | ||
| # MSTEST0058: Avoid assertions in `catch` blocks | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|----------------------------------------------------| | ||
| | **Rule ID** | MSTEST0058 | | ||
| | **Title** | Avoid assertions in catch blocks | | ||
| | **Category** | Usage | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default** | Yes | | ||
| | **Default severity** | Info | | ||
| | **Introduced in version** | 4.1.0 | | ||
| | **Is there a code fix** | No | | ||
|
|
||
| ## Cause | ||
|
|
||
| A test method contains assertion statements within a `catch` block. | ||
|
|
||
| ## Rule description | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| ```csharp | ||
| [TestClass] | ||
| public class TestClass | ||
| { | ||
| [TestMethod] | ||
| public void TestMethod() | ||
| { | ||
| try | ||
| { | ||
| // Code that might throw. | ||
| DoSomethingThatMightThrow(); | ||
| } | ||
| catch | ||
| { | ||
| Assert.Fail("Exception was thrown"); // Violation | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| Use `Assert.ThrowsException` or related assertion methods to test for exceptions. | ||
|
|
||
| ```csharp | ||
| [TestClass] | ||
| public class TestClass | ||
| { | ||
| [TestMethod] | ||
| public void TestMethod_ThrowsException() | ||
| { | ||
| Assert.Throws<InvalidOperationException>(() => DoSomethingThatMightThrow()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestMethod_DoesNotThrow() | ||
| { | ||
| // If no exception is expected, just call the method directly | ||
| DoSomethingThatMightNotThrow(); | ||
| // Add positive assertions here | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| 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. | ||
|
|
||
| ## 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. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable MSTEST0058 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore MSTEST0058 | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ```ini | ||
| [*.{cs,vb}] | ||
| dotnet_diagnostic.MSTEST0058.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| --- | ||
| title: "MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes" | ||
| description: "Learn about code analysis rule MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes" | ||
| ms.date: 12/29/2025 | ||
| f1_keywords: | ||
| - MSTEST0059 | ||
| - UseParallelizeAttributeAnalyzer | ||
| helpviewer_keywords: | ||
| - UseParallelizeAttributeAnalyzer | ||
| - MSTEST0059 | ||
| author: evangelink | ||
| ms.author: amauryleve | ||
| ai-usage: ai-assisted | ||
| dev_langs: | ||
| - CSharp | ||
| --- | ||
| # MSTEST0059: Do not use both Parallelize and DoNotParallelize attributes | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|----------------------------------------------------| | ||
| | **Rule ID** | MSTEST0059 | | ||
| | **Title** | Do not use both Parallelize and DoNotParallelize attributes | | ||
| | **Category** | Usage | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default** | Yes | | ||
| | **Default severity** | Warning | | ||
| | **Introduced in version** | 4.1.0 | | ||
| | **Is there a code fix** | No | | ||
|
|
||
| ## Cause | ||
|
|
||
| An assembly contains both <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute> attributes. | ||
|
|
||
| ## Rule description | ||
|
|
||
| The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelizeAttribute> attributes are mutually exclusive. Having both attributes in the same assembly creates conflicting configuration that can lead to unpredictable test execution behavior. You should choose one parallelization strategy for your test assembly. | ||
|
|
||
| ```csharp | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [assembly: Parallelize(Workers = 2, Scope = ExecutionScope.MethodLevel)] // Violation | ||
| [assembly: DoNotParallelize] | ||
| ``` | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| Remove one of the conflicting attributes based on your intended parallelization strategy. | ||
|
|
||
| If you want parallel execution: | ||
|
|
||
| ```csharp | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [assembly: Parallelize(Workers = 2, Scope = ExecutionScope.MethodLevel)] | ||
| ``` | ||
|
|
||
| If you want sequential execution: | ||
|
|
||
| ```csharp | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [assembly: DoNotParallelize] | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| Do not suppress warnings from this rule. Having both attributes creates ambiguous test configuration that should be resolved. | ||
|
|
||
| ## 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. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable MSTEST0059 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore MSTEST0059 | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ```ini | ||
| [*.{cs,vb}] | ||
| dotnet_diagnostic.MSTEST0059.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../../../fundamentals/code-analysis/suppress-warnings.md). |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.