Skip to content

Commit d0b9266

Browse files
authored
[rel/3.6] Fix MSTEST0030 to correctly handle all methods (#3974)
1 parent b170f00 commit d0b9266

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/Analyzers/MSTest.Analyzers/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
7777

7878
bool hasTestMethod = false;
7979
INamedTypeSymbol? currentType = namedTypeSymbol;
80-
do
80+
while (currentType is not null && !hasTestMethod)
8181
{
8282
foreach (ISymbol classMember in currentType.GetMembers())
8383
{
@@ -94,16 +94,10 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
9494
break;
9595
}
9696
}
97-
98-
if (!hasTestMethod)
99-
{
100-
break;
101-
}
10297
}
10398

10499
currentType = currentType.BaseType;
105100
}
106-
while (currentType is not null && !hasTestMethod);
107101

108102
if (!hasTestMethod)
109103
{

test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,28 @@ public void TestMethod1()
162162

163163
await VerifyCS.VerifyAnalyzerAsync(code);
164164
}
165+
166+
public async Task WhenClassHasTestInitializeAndThenTestMethod_Diagnostic()
167+
{
168+
string code = """
169+
using Microsoft.VisualStudio.TestTools.UnitTesting;
170+
171+
public class [|TestClass|]
172+
{
173+
[TestInitialize]
174+
public void Initialize()
175+
{
176+
177+
}
178+
179+
[TestMethod]
180+
public void TestMethod1()
181+
{
182+
183+
}
184+
}
185+
""";
186+
187+
await VerifyCS.VerifyAnalyzerAsync(code);
188+
}
165189
}

0 commit comments

Comments
 (0)