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.
MSTest analysis ("MSTESTxxxx") rules inspect your C# or Visual Basic code for security, performance, design and other issues.
Tip
If you're using Visual Studio, many analyzer rules have associated code fixes that you can apply to correct the problem. Code fixes are shown in the light bulb icon menu.
The rules are organized into categories such as performance usage...
Starting with MSTest.TestFramework 3.7, the MSTest.Analyzers NuGet package is a dependency of the framework. For earlier versions, you need to use MSTest metapackage or add a package reference for MSTest.Analyzers explicitly.
MSTestAnalysisMode
Starting with MSTest 3.8, an MSBuild property named MSTestAnalysisMode is available to determine which analyzers are enabled at which severity.
Tip
To see which rules are enabled at which severity for each mode, you can navigate to the package of the version of interest in NuGet cache, locate the globalconfigs directory, and open the .globalconfig file corresponding to the analysis mode.
For more information on finding the NuGet cache directory, see Managing the global packages, cache, and temp folders. From that directory, locate mstest.analyzers directory, then the version (3.8 and higher), then globalconfigs.
Alternatively, you can download the NuGet package of the version of interest from nuget.org and view it in NuGet Package Explorer (Windows app), or view directly in the web app version of NuGet Package Explorer.
The available values for this property:
None
This value sets all analyzers to none severity, disabling all of them. You can then enable individual analyzers using .editorconfig or .globalconfig files.
Default
This setting follows the default documented behavior for each rule.
- Rules that are enabled by default will use their default severity.
- Rules that are disabled by default will use
noneseverity.
Note
Rules that are enabled by default as warnings are violations that are expected to cause issues at runtime.
Recommended
This is the mode most developers are expected to use. Rules that are enabled by default with Info (suggestion) severity are escalated to warnings. The following rules are escalated to errors in both Recommended and All modes:
All
This mode is more aggressive than Recommended. All rules are enabled as warnings. In addition, the following rules are escalated to errors:
Note
The following rules are completely opt-in and are not enabled in Default, Recommended, or All modes:
Rules by category
The analyzer rules are organized into the following categories:
Design rules
Design rules help you create and maintain test suites that adhere to proper design and good practices.
Performance rules
Performance rules support high-performance testing.
Suppression rules
Suppression rules support suppressing diagnostics from other rules.
Usage rules
Usage rules support proper usage of MSTest.
Rules by concept
Find rules organized by common testing scenarios and concepts:
Test structure & attributes
Rules that help ensure your test classes and methods are properly structured and decorated:
- MSTEST0002 - Test class should be valid
- MSTEST0003 - Test method should be valid
- MSTEST0004 - Public types should be test classes
- MSTEST0007 - Use attribute on test method
- MSTEST0016 - Test class should have test method
- MSTEST0029 - Public method should be test method
- MSTEST0030 - Type containing test method should be a test class
- MSTEST0036 - Do not use shadowing
- MSTEST0041 - Use condition-based attributes with test class
- MSTEST0044 - Prefer TestMethod over DataTestMethod
- MSTEST0056 - TestMethodAttribute should set DisplayName correctly
- MSTEST0057 - TestMethodAttribute should propagate source information
- MSTEST0060 - Duplicate TestMethodAttribute
- MSTEST0063 - Test class should have valid constructor
Related documentation: Write tests with MSTest, Attributes
Async/await patterns
Rules for writing asynchronous test code correctly:
- MSTEST0013 - AssemblyCleanup should be valid (includes async rules)
- MSTEST0027 - Suppress async suffix for test methods
- MSTEST0028 - Suppress async suffix for test fixture methods
- MSTEST0039 - Use newer Assert.Throws methods (async variants)
- MSTEST0040 - Avoid using asserts in async void context
- MSTEST0045 - Use cooperative cancellation for timeout
- MSTEST0049 - Flow TestContext CancellationToken
- MSTEST0054 - Use CancellationToken property
Related documentation: TestContext
Data-driven testing
Rules for working with data-driven test scenarios:
- MSTEST0007 - Use attribute on test method
- MSTEST0014 - DataRow should be valid
- MSTEST0018 - DynamicData should be valid
- MSTEST0042 - Duplicate DataRow
- MSTEST0052 - Avoid explicit DynamicDataSourceType
- MSTEST0062 - Avoid out/ref test method parameters
Related documentation: Attributes used for data-driven testing
Lifecycle & initialization
Rules for test initialization, cleanup, and lifecycle management:
- MSTEST0008 - TestInitialize should be valid
- MSTEST0009 - TestCleanup should be valid
- MSTEST0010 - ClassInitialize should be valid
- MSTEST0011 - ClassCleanup should be valid
- MSTEST0012 - AssemblyInitialize should be valid
- MSTEST0013 - AssemblyCleanup should be valid
- MSTEST0019 - Prefer TestInitialize over constructors
- MSTEST0020 - Prefer constructors over TestInitialize
- MSTEST0021 - Prefer Dispose over TestCleanup
- MSTEST0022 - Prefer TestCleanup over Dispose
- MSTEST0034 - Use ClassCleanupBehavior.EndOfClass
- MSTEST0050 - Global test fixture should be valid
Related documentation: Lifecycle
Assertions
Rules for using assertion methods correctly and effectively:
- MSTEST0006 - Avoid ExpectedException attribute
- MSTEST0014 - DataRow should be valid
- MSTEST0017 - Assertion args should be passed in correct order
- MSTEST0023 - Do not negate boolean assertion
- MSTEST0025 - Prefer Assert.Fail over always-false conditions
- MSTEST0026 - Assertion args should avoid conditional access
- MSTEST0032 - Review always-true assert condition
- MSTEST0037 - Use proper assert methods
- MSTEST0038 - Avoid Assert.AreSame with value types
- MSTEST0039 - Use newer Assert.Throws methods
- MSTEST0046 - Use Assert instead of StringAssert
- MSTEST0051 - Assert.Throws should contain single statement
- MSTEST0053 - Avoid Assert format parameters
- MSTEST0058 - Avoid asserts in catch blocks
Related documentation: Assertions
TestContext
Rules for properly using the TestContext object:
- MSTEST0005 - TestContext should be valid
- MSTEST0024 - Do not store static TestContext
- MSTEST0033 - Suppress non-nullable reference not initialized warning
- MSTEST0048 - TestContext property usage
- MSTEST0049 - Flow TestContext CancellationToken
- MSTEST0054 - Use CancellationToken property
Related documentation: TestContext
Test configuration
Rules for configuring test execution, parallelization, and other test settings:
- MSTEST0001 - Use Parallelize attribute
- MSTEST0015 - Test method should not be ignored
- MSTEST0031 - Do not use System.ComponentModel.DescriptionAttribute
- MSTEST0035 - Use DeploymentItem with test method or test class
- MSTEST0043 - Use retry attribute on test method
- MSTEST0045 - Use cooperative cancellation for timeout
- MSTEST0055 - Do not ignore string method return value
- MSTEST0059 - Use Parallelize attribute correctly
- MSTEST0061 - Use OSCondition attribute instead of runtime check
Related documentation: Configure MSTest, Running tests
All rules (quick reference)
| Rule ID | Category | Title | Default Severity |
|---|---|---|---|
| MSTEST0001 | Performance | Use Parallelize attribute | Info |
| MSTEST0002 | Usage | Test class should be valid | Warning |
| MSTEST0003 | Usage | Test method should be valid | Warning → Error* |
| MSTEST0004 | Design | Public types should be test classes | Info |
| MSTEST0005 | Usage | TestContext should be valid | Warning |
| MSTEST0006 | Design | Avoid ExpectedException attribute | Info |
| MSTEST0007 | Usage | Use attribute on test method | Warning |
| MSTEST0008 | Usage | TestInitialize should be valid | Warning |
| MSTEST0009 | Usage | TestCleanup should be valid | Warning |
| MSTEST0010 | Usage | ClassInitialize should be valid | Warning |
| MSTEST0011 | Usage | ClassCleanup should be valid | Warning |
| MSTEST0012 | Usage | AssemblyInitialize should be valid | Warning |
| MSTEST0013 | Usage | AssemblyCleanup should be valid | Warning |
| MSTEST0014 | Usage | DataRow should be valid | Warning |
| MSTEST0015 | Design | Test method should not be ignored | None (opt-in) |
| MSTEST0016 | Design | Test class should have test method | Info |
| MSTEST0017 | Usage | Assertion args should be passed in correct order | Info |
| MSTEST0018 | Usage | DynamicData should be valid | Warning |
| MSTEST0019 | Design | Prefer TestInitialize over constructors | None (opt-in) |
| MSTEST0020 | Design | Prefer constructors over TestInitialize | None (opt-in) |
| MSTEST0021 | Design | Prefer Dispose over TestCleanup | None (opt-in) |
| MSTEST0022 | Design | Prefer TestCleanup over Dispose | None (opt-in) |
| MSTEST0023 | Usage | Do not negate boolean assertion | Info |
| MSTEST0024 | Usage | Do not store static TestContext | Warning |
| MSTEST0025 | Design | Prefer Assert.Fail over always-false conditions | Info |
| MSTEST0026 | Usage | Assertion args should avoid conditional access | Info |
| MSTEST0027 | Suppression | Suppress async suffix for test methods | N/A |
| MSTEST0028 | Suppression | Suppress async suffix for test fixture methods | N/A |
| MSTEST0029 | Design | Public method should be test method | Info |
| MSTEST0030 | Usage | Type containing test method should be a test class | Warning |
| MSTEST0031 | Usage | Do not use System.ComponentModel.DescriptionAttribute | Info |
| MSTEST0032 | Usage | Review always-true assert condition | Info |
| MSTEST0033 | Suppression | Suppress non-nullable reference not initialized | N/A |
| MSTEST0034 | Usage | Use ClassCleanupBehavior.EndOfClass | Info |
| MSTEST0035 | Usage | Use DeploymentItem with test method or test class | Info |
| MSTEST0036 | Design | Do not use shadowing | Warning |
| MSTEST0037 | Usage | Use proper assert methods | Info |
| MSTEST0038 | Usage | Avoid Assert.AreSame with value types | Info |
| MSTEST0039 | Usage | Use newer Assert.Throws methods | Info |
| MSTEST0040 | Usage | Avoid using asserts in async void context | Warning |
| MSTEST0041 | Usage | Use condition-based attributes with test class | Warning |
| MSTEST0042 | Usage | Duplicate DataRow | Warning |
| MSTEST0043 | Usage | Use retry attribute on test method | Warning → Error* |
| MSTEST0044 | Design | Prefer TestMethod over DataTestMethod | Info |
| MSTEST0045 | Design | Use cooperative cancellation for timeout | Info |
| MSTEST0046 | Usage | Use Assert instead of StringAssert | Info |
| MSTEST0048 | Usage | TestContext property usage | Warning |
| MSTEST0049 | Usage | Flow TestContext CancellationToken | Info |
| MSTEST0050 | Usage | Global test fixture should be valid | Warning |
| MSTEST0051 | Usage | Assert.Throws should contain single statement | Info |
| MSTEST0052 | Usage | Avoid explicit DynamicDataSourceType | Info |
| MSTEST0053 | Usage | Avoid Assert format parameters | Info |
| MSTEST0054 | Usage | Use CancellationToken property | Info |
| MSTEST0055 | Usage | Do not ignore string method return value | Warning |
| MSTEST0056 | Usage | TestMethodAttribute should set DisplayName correctly | Info |
| MSTEST0057 | Usage | TestMethodAttribute should propagate source information | Warning |
| MSTEST0058 | Usage | Avoid asserts in catch blocks | Info |
| MSTEST0059 | Usage | Use Parallelize attribute correctly | Warning |
| MSTEST0060 | Usage | Duplicate TestMethodAttribute | Warning |
| MSTEST0061 | Usage | Use OSCondition attribute instead of runtime check | Info |
| MSTEST0062 | Usage | Avoid out/ref test method parameters | Warning |
| MSTEST0063 | Usage | Test class should have valid constructor | Warning |
* Escalated to Error in Recommended and All modes.
MSTESTEXP
Several APIs of MSTest are decorated with the ExperimentalAttribute. This attribute indicates that the API is experimental and may be removed or changed in future versions of MSTest. The attribute is used to identify APIs that aren't yet stable and may not be suitable for production use.
The MSTESTEXP diagnostic alerts you to use of an experimental API in your code. To suppress this diagnostic with the SuppressMessageAttribute, add the following code to your project:
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("MSTESTEXP", "Justification")]
Alternatively, you can suppress this diagnostic with preprocessor directive by adding the following code to your project:
#pragma warning disable MSTESTEXP
// API that is causing the warning.
#pragma warning restore MSTESTEXP