Skip to content

Commit b095754

Browse files
committed
Fix TestInitialize and TestCleanup analyzers to allow generic class (#3280)
1 parent 71a7f31 commit b095754

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/Analyzers/MSTest.Analyzers/TestCleanupShouldBeValidAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
5555
var methodSymbol = (IMethodSymbol)context.Symbol;
5656
if (methodSymbol.IsTestCleanupMethod(testCleanupAttributeSymbol)
5757
&& !methodSymbol.HasValidFixtureMethodSignature(taskSymbol, valueTaskSymbol, canDiscoverInternals, shouldBeStatic: false,
58-
allowGenericType: false, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
58+
allowGenericType: true, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
5959
{
6060
context.ReportDiagnostic(isFixable
6161
? methodSymbol.CreateDiagnostic(Rule, methodSymbol.Name)

src/Analyzers/MSTest.Analyzers/TestInitializeShouldBeValidAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
5555
var methodSymbol = (IMethodSymbol)context.Symbol;
5656
if (methodSymbol.IsTestInitializeMethod(testInitializeAttributeSymbol)
5757
&& !methodSymbol.HasValidFixtureMethodSignature(taskSymbol, valueTaskSymbol, canDiscoverInternals, shouldBeStatic: false,
58-
allowGenericType: false, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
58+
allowGenericType: true, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
5959
{
6060
context.ReportDiagnostic(isFixable
6161
? methodSymbol.CreateDiagnostic(Rule, methodSymbol.Name)

test/UnitTests/MSTest.Analyzers.UnitTests/TestCleanupShouldBeValidAnalyzerTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,22 @@ public void TestCleanup()
515515

516516
await VerifyCS.VerifyAnalyzerAsync(code);
517517
}
518+
519+
public async Task WhenTestCleanupIsOnGenericClass_NoDiagnostic()
520+
{
521+
string code = """
522+
using Microsoft.VisualStudio.TestTools.UnitTesting;
523+
524+
[TestClass]
525+
public class MyTestClass<T>
526+
{
527+
[TestCleanup]
528+
public void TestCleanup()
529+
{
530+
}
531+
}
532+
""";
533+
534+
await VerifyCS.VerifyAnalyzerAsync(code);
535+
}
518536
}

test/UnitTests/MSTest.Analyzers.UnitTests/TestInitializeShouldBeValidAnalyzerTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,22 @@ public void TestInitialize()
513513

514514
await VerifyCS.VerifyAnalyzerAsync(code);
515515
}
516+
517+
public async Task WhenTestInitializeIsOnGenericClass_NoDiagnostic()
518+
{
519+
string code = """
520+
using Microsoft.VisualStudio.TestTools.UnitTesting;
521+
522+
[TestClass]
523+
public class MyTestClass<T>
524+
{
525+
[TestInitialize]
526+
public void TestInitialize()
527+
{
528+
}
529+
}
530+
""";
531+
532+
await VerifyCS.VerifyAnalyzerAsync(code);
533+
}
516534
}

0 commit comments

Comments
 (0)