MSTest 使用规则

支持正确使用 MSTest 的规则。

Identifier Name Description
MSTEST0002 TestClassShouldBeValidAnalyzer 测试类不遵循所需测试类布局的单个或多个点。
MSTEST0003 TestMethodShouldBeValidAnalyzer 测试方法不遵循所需测试方法布局的单个或多个点。
MSTEST0005 TestContextShouldBeValidAnalyzer 测试上下文属性不遵循所需测试上下文布局的单个或多个点。
MSTEST0007 UseAttributeOnTestMethodAnalyzer 未标记 TestMethodAttribute 的方法已应用一个或多个测试属性。
MSTEST0008 TestInitializeShouldBeValidAnalyzer 标记为 [TestInitialize] 的方法应具有有效的布局。
MSTEST0009 TestCleanupShouldBeValidAnalyzer 标记为 [TestCleanup] 的方法应具有有效的布局。
MSTEST0010 ClassInitializeShouldBeValidAnalyzer 标记为 [ClassInitialize] 的方法应具有有效的布局。
MSTEST0011 ClassCleanupShouldBeValidAnalyzer 标记为 [ClassCleanup] 的方法应具有有效的布局。
MSTEST0012 AssemblyInitializeShouldBeValidAnalyzer 标记为 [AssemblyInitialize] 的方法应具有有效的布局。
MSTEST0013 AssemblyCleanupShouldBeValidAnalyzer 标记为 [AssemblyCleanup] 的方法应具有有效的布局。
MSTEST0014 DataRowShouldBeValidAnalyzer [DataRow] 的实例没有遵循所需 DataRow 布局的一个或多个点。
MSTEST0017 AssertionArgsShouldBePassedInCorrectOrder Assert.AreEqualAssert.AreNotEqualAssert.AreSameAssert.AreNotSame 的调用遵循以下一个或多个模式时,此规则引发问题:

- actual 参数是常量或文本值
- actual参数变量以expected_expectedExpected开头
- expectednotExpected 参数
变量以actual开头
- actual 不是局部变量
MSTEST0018 DynamicDataShouldBeValidAnalyzer 标记为 [DynamicData] 的方法应具有有效的布局。
MSTEST0023 DoNotNegateBooleanAssertionAnalyzer 当调用 Assert.IsTrueAssert.IsFalse 包含否定参数时,此规则将引发诊断。
MSTEST0024 DoNotStoreStaticTestContextAnalyzer 完成对 static 参数 TestContext 成员的分配时,此规则将引发诊断。
MSTEST0026 AssertionArgsShouldAvoidConditionalAccessRuleId 在将包含 null 条件运算符(?.)?[] 的参数传递给以下断言方法时,此规则将引发诊断:

- Assert.IsTrue
- Assert.IsFalse
- Assert.AreEqual
- Assert.AreNotEqual
- Assert.AreSame
- Assert.AreNotSame
- CollectionAssert.AreEqual
- CollectionAssert.AreNotEqual
- CollectionAssert.AreEquivalent
- CollectionAssert.AreNotEquivalent
- CollectionAssert.Contains
- CollectionAssert.DoesNotContain
- CollectionAssert.AllItemsAreNotNull
- CollectionAssert.AllItemsAreUnique
- CollectionAssert.AllItemsAreInstancesOfType
- CollectionAssert.IsSubsetOf
- CollectionAssert.IsNotSubsetOf
- StringAssert.Contains
- StringAssert.StartsWith
- StringAssert.EndsWith
- StringAssert.Matches
- StringAssert.DoesNotMatch
MSTEST0030 TypeContainingTestMethodShouldBeATestClass 包含 [TestMethod] 的类型应标记为 [TestClass],否则测试方法将在无提示的情况下被忽略。
MSTEST0031 DoNotUseSystemDescriptionAttribute “System.ComponentModel.DescriptionAttribute”在测试上下文中不起作用。
MSTEST0032 ReviewAlwaysTrueAssertConditionAnalyzer 当对断言的调用生成始终为 true 的条件时,此规则会引发诊断。
MSTEST0034 UseClassCleanupBehaviorEndOfClass 如果没有随 ClassCleanupBehavior.EndOfClass 设置 [ClassCleanup],则此规则会引发诊断。
MSTEST0035 UseDeploymentItemWithTestMethodOrTestClassTitle 未在测试类或测试方法上设置 [DeploymentItem] 时,此规则会引发诊断。
MSTEST0037 UseProperAssertMethodsAnalyzer 当有更好的替代方法时,以特定方式使用 Assert 方法。
MSTEST0038 AvoidAssertAreSameWithValueTypesAnalyzer 对作为值类型的一个或两个参数使用 Assert.AreSameAssert.AreNotSame
MSTEST0039 UseNewerAssertThrowsAnalyzer 不再推荐使用 Assert.ThrowsExceptionAssert.ThrowsExceptionAsync这两种标号。
MSTEST0040 AvoidUsingAssertsInAsyncVoidContextAnalyzer async void 方法、本地函数或 lambda 中使用任何断言方法。
MSTEST0041 UseConditionBaseWithTestClassAnalyzer 在未标记为 ConditionBaseAttribute的类上使用继承自 TestClassAttribute 的属性。
MSTEST0042 DuplicateDataRowAnalyzer 测试方法包含两个或多个等效的DataRow属性。
MSTEST0043 UseRetryWithTestMethodAnalyzer 方法具有派生自 RetryBaseAttribute 的属性,并且没有派生自 TestMethodAttribute的属性。
MSTEST0046 字符串断言到断言分析器 (StringAssertToAssertAnalyzer) 测试方法使用 StringAssert 方法,而不是等效 Assert 的方法。
MSTEST0048 TestContextPropertyUsageAnalyzer(测试上下文属性使用分析器) 固定方法(具有AssemblyInitializeAttributeAssemblyCleanupAttributeClassInitializeAttribute、或ClassCleanupAttribute)访问受限TestContext属性。
MSTEST0049 流程测试上下文取消令牌分析器 当被调用的方法具有接受CancellationToken的参数或重载时,测试上下文中的方法调用不会使用从TestContext获得的CancellationToken
MSTEST0050 GlobalTestFixtureShouldBeValidAnalyzer (全局测试夹具有效性分析器) 全局测试装置方法(标有 GlobalTestInitializeAttributeGlobalTestCleanupAttribute)不遵循所需的布局或配置无效。
MSTEST0051 AssertThrowsShouldContainSingleStatementAnalyzer (Note: Since this is a technical term/name, it is appropriate to leave it untranslated if it is commonly used as-is within technical contexts for the target audience.) 调用ThrowsThrowsAsyncThrowsExactlyThrowsExactlyAsync时,操作委托中包含多个语句。
MSTEST0052 避免显式动态数据源类型分析器 DynamicDataAttribute 显式指定 PropertyMethod,而不是使用默认的 AutoDetect
MSTEST0053 AvoidAssertFormatParametersAnalyzer 断言方法调用使用 messageparameters 参数进行字符串格式设置,而不是使用字符串内插。
MSTEST0054 使用取消令牌属性分析器 (UseCancellationTokenPropertyAnalyzer) 使用TestContext.CancellationTokenSource.Token访问CancellationToken,而不是使用TestContext.CancellationToken属性。
MSTEST0055 IgnoreStringMethodReturnValueAnalyzer 调用 string.Containsstring.StartsWithstring.EndsWith 后,其返回值被忽略。