Skip to content

Commit c0af0d1

Browse files
EvangelinkCopilot
andauthored
test: add edge case tests for TestClassShouldHaveTestMethodAnalyzer (MSTEST0016)
Add 3 new edge case tests: - WhenNonStaticTestClassWithGlobalTestCleanup_DoesNotHaveTestMethod_Diagnostic: mirrors the existing GlobalTestInitialize counterpart, confirms GlobalTestCleanup only exempts static classes - WhenTestClassHasDataTestMethod_NoDiagnostic: verifies DataTestMethodAttribute (inherits from TestMethodAttribute) satisfies the Inherits() check - WhenTestClassHasDerivedTestMethodAttribute_NoDiagnostic: verifies a custom TestMethodAttribute subclass satisfies the Inherits() check Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e3af1e5 commit c0af0d1

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

test/UnitTests/MSTest.Analyzers.UnitTests/TestClassShouldHaveTestMethodAnalyzerTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,74 @@ await VerifyCS.VerifyAnalyzerAsync(
348348
.WithLocation(0)
349349
.WithArguments("MyTestClass"));
350350
}
351+
352+
[TestMethod]
353+
public async Task WhenNonStaticTestClassWithGlobalTestCleanup_DoesNotHaveTestMethod_Diagnostic()
354+
{
355+
// GlobalTestCleanup only exempts static test classes from needing a test method,
356+
// not non-static ones. This mirrors the existing GlobalTestInitialize counterpart.
357+
string code = """
358+
using Microsoft.VisualStudio.TestTools.UnitTesting;
359+
360+
[TestClass]
361+
public class {|#0:MyTestClass|}
362+
{
363+
[GlobalTestCleanup]
364+
public static void GlobalTestCleanup(TestContext context)
365+
{
366+
}
367+
}
368+
""";
369+
370+
await VerifyCS.VerifyAnalyzerAsync(
371+
code,
372+
VerifyCS.Diagnostic(TestClassShouldHaveTestMethodAnalyzer.TestClassShouldHaveTestMethodRule)
373+
.WithLocation(0)
374+
.WithArguments("MyTestClass"));
375+
}
376+
377+
[TestMethod]
378+
public async Task WhenTestClassHasDataTestMethod_NoDiagnostic()
379+
{
380+
// DataTestMethodAttribute inherits from TestMethodAttribute, so the Inherits() check
381+
// should recognise it as a test method and suppress the diagnostic.
382+
string code = """
383+
using Microsoft.VisualStudio.TestTools.UnitTesting;
384+
385+
[TestClass]
386+
public class MyTestClass
387+
{
388+
[DataTestMethod]
389+
[DataRow(1)]
390+
public void MyTestMethod(int x)
391+
{
392+
}
393+
}
394+
""";
395+
396+
await VerifyCS.VerifyAnalyzerAsync(code);
397+
}
398+
399+
[TestMethod]
400+
public async Task WhenTestClassHasDerivedTestMethodAttribute_NoDiagnostic()
401+
{
402+
// A custom attribute that derives from TestMethodAttribute should satisfy the
403+
// Inherits() check and prevent the "no test method" diagnostic.
404+
string code = """
405+
using Microsoft.VisualStudio.TestTools.UnitTesting;
406+
407+
public class MyTestMethodAttribute : TestMethodAttribute { }
408+
409+
[TestClass]
410+
public class MyTestClass
411+
{
412+
[MyTestMethod]
413+
public void MyTestMethod()
414+
{
415+
}
416+
}
417+
""";
418+
419+
await VerifyCS.VerifyAnalyzerAsync(code);
420+
}
351421
}

0 commit comments

Comments
 (0)