From d0b926673a955a734d2339dc8ad8a44822f24a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Wed, 30 Oct 2024 10:51:53 +0100 Subject: [PATCH] [rel/3.6] Fix MSTEST0030 to correctly handle all methods (#3974) --- ...ingTestMethodShouldBeATestClassAnalyzer.cs | 8 +------ ...ingTestMethodShouldBeATestClassAnalyzer.cs | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Analyzers/MSTest.Analyzers/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs b/src/Analyzers/MSTest.Analyzers/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs index f89aa288f8..da57e4d3cc 100644 --- a/src/Analyzers/MSTest.Analyzers/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs +++ b/src/Analyzers/MSTest.Analyzers/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs @@ -77,7 +77,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo bool hasTestMethod = false; INamedTypeSymbol? currentType = namedTypeSymbol; - do + while (currentType is not null && !hasTestMethod) { foreach (ISymbol classMember in currentType.GetMembers()) { @@ -94,16 +94,10 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo break; } } - - if (!hasTestMethod) - { - break; - } } currentType = currentType.BaseType; } - while (currentType is not null && !hasTestMethod); if (!hasTestMethod) { diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs index cad0ffead4..a743894786 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs @@ -162,4 +162,28 @@ public void TestMethod1() await VerifyCS.VerifyAnalyzerAsync(code); } + + public async Task WhenClassHasTestInitializeAndThenTestMethod_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + public class [|TestClass|] + { + [TestInitialize] + public void Initialize() + { + + } + + [TestMethod] + public void TestMethod1() + { + + } + } + """; + + await VerifyCS.VerifyAnalyzerAsync(code); + } }