diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/TestMethodShouldNotBeIgnoredAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/TestMethodShouldNotBeIgnoredAnalyzerTests.cs index 9c66e4a8f0..88f0c0825a 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/TestMethodShouldNotBeIgnoredAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/TestMethodShouldNotBeIgnoredAnalyzerTests.cs @@ -99,4 +99,107 @@ await VerifyCS.VerifyAnalyzerAsync( .WithLocation(0) .WithArguments("MyTestMethod")); } + + [TestMethod] + public async Task WhenDataTestMethodIsIgnored_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [Ignore] + [DataTestMethod] + [DataRow(1)] + public void {|#0:MyTestMethod|}(int value) + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(TestMethodShouldNotBeIgnoredAnalyzer.TestMethodShouldNotBeIgnoredRule) + .WithLocation(0) + .WithArguments("MyTestMethod")); + } + + [TestMethod] + public async Task WhenMultipleTestMethodsAndOnlyOneIsIgnored_DiagnosticOnlyForIgnoredMethod() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [TestMethod] + public void NotIgnoredMethod() + { + } + + [Ignore] + [TestMethod] + public void {|#0:IgnoredMethod|}() + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(TestMethodShouldNotBeIgnoredAnalyzer.TestMethodShouldNotBeIgnoredRule) + .WithLocation(0) + .WithArguments("IgnoredMethod")); + } + + [TestMethod] + public async Task WhenOnlyClassLevelIgnoreWithNoMethodLevelIgnore_NoDiagnostic() + { + // MSTEST0015 only checks for [Ignore] on the method itself. + // A class-level [Ignore] silences the whole class but is not flagged by this rule. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + [Ignore] + public class MyTestClass + { + [TestMethod] + public void MyTestMethod() + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } + + [TestMethod] + public async Task WhenIgnoredTestMethodHasMessage_Diagnostic() + { + // MSTEST0015 fires even when [Ignore] has a justification message. + // The presence of a message satisfies MSTEST0066 (IgnoreShouldHaveJustification), + // but the method is still ignored and MSTEST0015 still applies. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MyTestClass + { + [Ignore("Tracked by #12345")] + [TestMethod] + public void {|#0:MyTestMethod|}() + { + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync( + code, + VerifyCS.Diagnostic(TestMethodShouldNotBeIgnoredAnalyzer.TestMethodShouldNotBeIgnoredRule) + .WithLocation(0) + .WithArguments("MyTestMethod")); + } }