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