From 71b7d2aec471cb9cbc7ce3bd225991b8aa8cd9df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Sun, 21 Jun 2026 01:36:29 +0200 Subject: [PATCH] test: add edge case tests for UnusedParameterSuppressor (MSTEST0047) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add two missing edge case tests to verify the suppressor's exact boundaries: 1. TestMethodWithUnusedTestContext_DiagnosticIsNotSuppressed: verifies that a TestContext parameter in a [TestMethod] (not a fixture attribute) is NOT suppressed — [TestMethod] is absent from the suppressor's allowed-attribute list. 2. AssemblyInitializeWithUnusedNonTestContextParameter_DiagnosticIsNotSuppressed: verifies that a non-TestContext parameter (e.g. string) inside [AssemblyInitialize] is NOT suppressed — only TestContext parameters qualify. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../UnusedParameterSuppressorTests.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs index d620499902..16c5b52d5d 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs @@ -250,6 +250,96 @@ public void RegularMethod(TestContext {|#0:context|}) }.RunAsync(); } + [TestMethod] + public async Task TestMethodWithUnusedTestContext_DiagnosticIsNotSuppressed() + { + // [TestMethod] is not in the suppressor's fixture-attribute list, so even a TestContext + // parameter should not be suppressed. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class SomeClass + { + [TestMethod] + public void TestMethod(TestContext {|#0:context|}) + { + } + } + """; + + // Verify issue is reported without suppressor + await new VerifyCS.Test + { + TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(false), + }, + }.RunAsync(); + + // Verify issue is still reported with suppressor (not suppressed) + await new TestWithSuppressor + { + TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(false), + }, + }.RunAsync(); + } + + [TestMethod] + public async Task AssemblyInitializeWithUnusedNonTestContextParameter_DiagnosticIsNotSuppressed() + { + // The suppressor only suppresses TestContext parameters; a non-TestContext parameter in a + // fixture method (e.g. [AssemblyInitialize]) should not be suppressed. + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class SomeClass + { + [AssemblyInitialize] + public static void Initialize(string {|#0:message|}) + { + } + } + """; + + // Verify issue is reported without suppressor + await new VerifyCS.Test + { + TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("message") + .WithIsSuppressed(false), + }, + }.RunAsync(); + + // Verify issue is still reported with suppressor (not suppressed) + await new TestWithSuppressor + { + TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("message") + .WithIsSuppressed(false), + }, + }.RunAsync(); + } + [DiagnosticAnalyzer(LanguageNames.CSharp)] [SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1038:Compiler extensions should be implemented in assemblies with compiler-provided references", Justification = "For suppression test only.")] [SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1036:Specify analyzer banned API enforcement setting", Justification = "For suppression test only.")]