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] 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); + } }