Edit

Share via


MSTest usage rules

Usage rules support proper usage of MSTest attributes, methods, and patterns. These rules catch common mistakes and ensure your tests follow the framework's requirements and conventions.

Rules in this category

Rule ID Title Severity Fix Available
MSTEST0002 Test class should be valid. Warning Yes
MSTEST0003 Test method should be valid. Warning → Error* Yes
MSTEST0005 TestContext should be valid. Warning Yes
MSTEST0007 Use attribute on test method. Warning No
MSTEST0008 TestInitialize should be valid. Warning Yes
MSTEST0009 TestCleanup should be valid. Warning Yes
MSTEST0010 ClassInitialize should be valid. Warning Yes
MSTEST0011 ClassCleanup should be valid. Warning Yes
MSTEST0012 AssemblyInitialize should be valid. Warning Yes
MSTEST0013 AssemblyCleanup should be valid. Warning Yes
MSTEST0014 DataRow should be valid. Warning Yes
MSTEST0017 Assertion args should be passed in correct order. Info Yes
MSTEST0018 DynamicData should be valid. Warning Yes
MSTEST0023 Do not negate boolean assertion. Info Yes
MSTEST0024 Do not store static TestContext. Warning No
MSTEST0026 Assertion args should avoid conditional access. Info No
MSTEST0030 Type containing test method should be a test class. Warning Yes
MSTEST0031 Do not use System.ComponentModel.DescriptionAttribute. Info Yes
MSTEST0032 Review always-true assert condition. Info No
MSTEST0034 Use ClassCleanupBehavior.EndOfClass. Info Yes
MSTEST0035 Use DeploymentItem with test method or test class. Info No
MSTEST0037 Use proper assert methods. Info Yes
MSTEST0038 Avoid Assert.AreSame with value types. Info Yes
MSTEST0039 Use newer Assert.Throws methods. Info Yes
MSTEST0040 Avoid using asserts in async void context. Warning No
MSTEST0041 Use condition-based attributes with test class. Warning No
MSTEST0042 Duplicate DataRow. Warning No
MSTEST0043 Use retry attribute on test method. Warning → Error* Yes
MSTEST0046 Use Assert instead of StringAssert. Info Yes
MSTEST0048 TestContext property usage. Warning No
MSTEST0049 Flow TestContext CancellationToken. Info Yes
MSTEST0050 Global test fixture should be valid. Warning Yes
MSTEST0051 Assert.Throws should contain single statement. Info Yes
MSTEST0052 Avoid explicit DynamicDataSourceType. Info Yes
MSTEST0053 Avoid Assert format parameters. Info Yes
MSTEST0054 Use CancellationToken property. Info Yes
MSTEST0055 Do not ignore string method return value. Warning No
MSTEST0056 TestMethodAttribute should set DisplayName correctly. Info Yes
MSTEST0057 TestMethodAttribute should propagate source information. Warning No
MSTEST0058 Avoid asserts in catch blocks. Info No
MSTEST0059 Use Parallelize attribute correctly. Warning No
MSTEST0060 Duplicate TestMethodAttribute. Warning Yes
MSTEST0061 Use OSCondition attribute instead of runtime check. Info Yes
MSTEST0062 Avoid out/ref test method parameters. Warning Yes
MSTEST0063 Test class should have valid constructor. Warning No

* Escalated to Error in Recommended and All modes.

Common scenarios

Test structure validation

Ensure your test classes, methods, and fixtures follow MSTest requirements:

  • MSTEST0002: Test class layout requirements (for example, public, non-static).
  • MSTEST0003: Test method layout requirements (⚠️ escalated to Error).
  • MSTEST0030: Methods with [TestMethod] must be in a [TestClass].
  • MSTEST0063: Test class constructor validation.

Lifecycle methods

Validate initialization and cleanup methods:

Data-driven testing

Ensure data attributes are used correctly:

Writing better assertions

Rules for correct and effective assertion usage:

  • MSTEST0017: Pass expected/actual in correct order.
  • MSTEST0023: Don't negate conditions (use Assert.IsFalse directly).
  • MSTEST0026: Avoid null-conditional operators in assertions.
  • MSTEST0032: Review always-true conditions.
  • MSTEST0037: Use the most appropriate assert method.
  • MSTEST0038: Don't use AreSame with value types.
  • MSTEST0039: Use Assert.ThrowsExactly (newer API).
  • MSTEST0046: Prefer Assert over StringAssert.
  • MSTEST0051: Assert.Throws should test single statement.
  • MSTEST0053: Use string interpolation instead of format parameters.
  • MSTEST0058: Don't put assertions in catch blocks.

TestContext usage

Proper usage of the TestContext object:

  • MSTEST0005: TestContext property validation.
  • MSTEST0024: Don't store TestContext in static fields.
  • MSTEST0048: Restricted property access in fixtures.
  • MSTEST0049: Flow cancellation tokens from TestContext.
  • MSTEST0054: Use TestContext.CancellationToken property.

Async patterns

Rules for asynchronous test code:

  • MSTEST0040: Avoid asserts in async void methods.

Test configuration

  • MSTEST0031: Use proper attributes (not System.ComponentModel.Description).
  • MSTEST0035: DeploymentItem usage.
  • MSTEST0041: Condition attributes must be on test classes.
  • MSTEST0043: Retry attributes must be on test methods (⚠️ escalated to Error).
  • MSTEST0055: Don't ignore string method return values.
  • MSTEST0056: Set DisplayName properly on TestMethodAttribute.
  • MSTEST0057: Propagate source info in custom TestMethodAttribute.
  • MSTEST0059: Don't use both Parallelize and DoNotParallelize.
  • MSTEST0060: Avoid duplicate TestMethodAttribute.
  • MSTEST0061: Use OSCondition attribute for platform checks.