From 49f206c272f542019e8fb436e2910ec7b60b24f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Sat, 13 Jun 2026 01:44:12 +0200 Subject: [PATCH 1/2] test: add DataTestMethod edge cases for TypeContainingTestMethodShouldBeATestClassAnalyzer (MSTEST0030) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two new test cases verifying that DataTestMethodAttribute (which inherits TestMethodAttribute) is correctly recognized by the Inherits() check: 1. WhenNonTestClassHasDataTestMethod_Diagnostic – a non-test class with a [DataTestMethod] directly triggers the diagnostic and the fixer adds [TestClass]. 2. WhenNonTestClassInheritsDataTestMethodFromAbstractBase_Diagnostic – the inheritance walk correctly detects DataTestMethod from an abstract base class, firing the diagnostic only on the concrete derived class (abstract base is exempt per the IsAbstract guard). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...stMethodShouldBeATestClassAnalyzerTests.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs index bec343fbaf..ae719e5983 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs @@ -462,4 +462,72 @@ public void TestMethod1() await VerifyCS.VerifyCodeFixAsync(code, fixedCode); } + + [TestMethod] + public async Task WhenNonTestClassHasDataTestMethod_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public class [|MyTestClass|] + { + [DataTestMethod] + [DataRow(1)] + public void TestMethod1(int value) {} + } + """; + + string fixedCode = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [DataTestMethod] + [DataRow(1)] + public void TestMethod1(int value) {} + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, fixedCode); + } + + [TestMethod] + public async Task WhenNonTestClassInheritsDataTestMethodFromAbstractBase_Diagnostic() + { + // Abstract base is exempt from the diagnostic; derived non-test class + // that inherits [DataTestMethod] via the inheritance walk should fire. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public abstract class AbstractBase + { + [DataTestMethod] + [DataRow(1)] + public void TestMethod1(int value) {} + } + + public class [|MyTestClass|] : AbstractBase + { + } + """; + + string fixedCode = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public abstract class AbstractBase + { + [DataTestMethod] + [DataRow(1)] + public void TestMethod1(int value) {} + } + + [TestClass] + public class MyTestClass : AbstractBase + { + } + """; + + await VerifyCS.VerifyCodeFixAsync(code, fixedCode); + } } From 1847408cded31044253a357a017622af327c05ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Sun, 14 Jun 2026 21:06:44 +0200 Subject: [PATCH 2/2] Address review: rename DataTestMethod tests to match WhenClassWithoutTestAttribute_... convention Per #9092 self-review NIT (D16): the dominant convention in this file is `WhenClassWithoutTestAttribute_...` for diagnostic tests on non-test classes (lines 33, 60, 113, 193). Renamed the two new tests to follow the established pattern. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs index ae719e5983..2881b042f4 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzerTests.cs @@ -464,7 +464,7 @@ public void TestMethod1() } [TestMethod] - public async Task WhenNonTestClassHasDataTestMethod_Diagnostic() + public async Task WhenClassWithoutTestAttribute_HasDataTestMethod_Diagnostic() { string code = """ using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -493,7 +493,7 @@ public void TestMethod1(int value) {} } [TestMethod] - public async Task WhenNonTestClassInheritsDataTestMethodFromAbstractBase_Diagnostic() + public async Task WhenClassWithoutTestAttribute_InheritsDataTestMethodFromAbstractBase_Diagnostic() { // Abstract base is exempt from the diagnostic; derived non-test class // that inherits [DataTestMethod] via the inheritance walk should fire.