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