From c9ee4fac51ec4b6daa7de3970d015f461ec13335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Thu, 11 Jun 2026 01:38:36 +0200 Subject: [PATCH] test: add edge case tests for DoNotUseSystemDescriptionAttributeAnalyzer (MSTEST0031) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add two new test cases: - [DataTestMethod] + [System.ComponentModel.Description] → diagnostic (confirms Inherits() catches attributes derived from TestMethodAttribute) - [TestMethod] + MSTest's [Description] (no System.ComponentModel using) → no diagnostic (resolves to the correct MSTest attribute) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...SystemDescriptionAttributeAnalyzerTests.cs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/DoNotUseSystemDescriptionAttributeAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/DoNotUseSystemDescriptionAttributeAnalyzerTests.cs index 23c2ea4048..183725adc9 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/DoNotUseSystemDescriptionAttributeAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/DoNotUseSystemDescriptionAttributeAnalyzerTests.cs @@ -165,4 +165,65 @@ public void Method() await VerifyCS.VerifyAnalyzerAsync(code); } + + [TestMethod] + public async Task WhenDataTestMethodHasSystemDescriptionAttribute_Diagnostic() + { + // DataTestMethodAttribute inherits from TestMethodAttribute, so the Inherits() check should + // catch it and report the same diagnostic as for [TestMethod]. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [DataTestMethod] + [DataRow(1)] + [System.ComponentModel.Description("Description")] + public void [|MyTestMethod|](int x) + { + } + } + """; + + string fixedCode = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [DataTestMethod] + [DataRow(1)] + [Description("Description")] + public void MyTestMethod(int x) + { + } + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, fixedCode); + } + + [TestMethod] + public async Task WhenTestMethodHasMSTestDescriptionAttribute_NoDiagnostic() + { + // When only the MSTest namespace is in scope, the short-form [Description] resolves to + // Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute, not + // System.ComponentModel.DescriptionAttribute. The analyzer must not fire. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + [Description("Description")] + public void MyTestMethod() + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } }