Skip to content

[test-improver] test: add edge case tests for PublicTypeShouldBeTestClassAnalyzer (MSTEST0004)#8941

Merged
Evangelink merged 1 commit into
mainfrom
test-assist/public-type-test-class-edge-cases-6e0b8ff7f98c3d34
Jun 9, 2026
Merged

[test-improver] test: add edge case tests for PublicTypeShouldBeTestClassAnalyzer (MSTEST0004)#8941
Evangelink merged 1 commit into
mainfrom
test-assist/public-type-test-class-edge-cases-6e0b8ff7f98c3d34

Conversation

@Evangelink

Copy link
Copy Markdown
Member

🤖 Test Improver — automated AI assistant focused on improving tests for this repository.

Goal and Rationale

PublicTypeShouldBeTestClassAnalyzer (MSTEST0004) had 8 tests covering the basic "public class without [TestClass]" path and several well-known no-diagnostic cases (abstract, static, non-public, etc.), but missed four real-world scenarios:

  1. public record (record class) — records have TypeKind.Class in Roslyn; the analyzer should flag them just like plain classes, but there was no test confirming this.
  2. public record struct — value-type records have TypeKind.Struct; the analyzer explicitly skips non-Class types, so these should never be flagged.
  3. Nested public class inside an internal outer classGetResultantVisibility() returns Internal here, so the rule does not apply. Without a test, a future change to the visibility check could silently start flagging these.
  4. Nested public class inside a public outer class — both the outer and inner classes lack [TestClass] and have resultant-public visibility, so the analyzer should flag both. Verifies that the rule walks all named types, not just top-level ones.
  5. Nested public class inside a [TestClass] — the outer class is fine; the inner class has no [TestClass] and should be flagged. The fix correctly adds [TestClass] only to the inner class.

Approach

  • Added 5 new [TestMethod] test cases to PublicTypeShouldBeTestClassAnalyzerTests.
  • Each test uses VerifyCS.VerifyCodeFixAsync (following the existing pattern) to assert both the diagnostic location and the code-fix output.
  • No production code changes.

Test Status

Test run summary: Passed!
  total: 13
  failed: 0
  succeeded: 13
  skipped: 0
  duration: 6s 633ms

✅ All 13 tests pass (MSTest.Analyzers.UnitTests, net8.0, Debug — 8 original + 5 new).

Reproducibility

./build.sh --restore   # first run only
.dotnet/dotnet test test/UnitTests/MSTest.Analyzers.UnitTests/MSTest.Analyzers.UnitTests.csproj \
  -f net8.0 --no-build -c Debug \
  --filter "ClassName=MSTest.Analyzers.UnitTests.PublicTypeShouldBeTestClassAnalyzerTests"

Trade-offs

  • Tests only exercise existing code paths in the analyzer — no production changes, no maintenance burden beyond the tests themselves.
  • The nested-class scenarios document subtleties of GetResultantVisibility() that would otherwise be implicit and easy to break in a refactor.

Generated by Test Improver · sonnet46 6.2M ·

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/test-improver.md@main

…TEST0004)

Add 5 new test cases covering scenarios not previously tested:
- Public record class gets diagnostic (TypeKind.Class)
- Public record struct does not (TypeKind.Struct)
- Public class nested inside internal class does not (resultant visibility is internal)
- Public class nested inside public non-test class does (both outer and inner are flagged)
- Public class nested inside [TestClass] does (outer is fine, inner is flagged)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 8, 2026 23:40
@Evangelink Evangelink added type/automation Created or maintained by an agentic workflow. type/test-gap Missing or insufficient tests. labels Jun 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the MSTest analyzer unit test suite for MSTEST0004 (PublicTypeShouldBeTestClassAnalyzer) by adding coverage for several real-world type-shape and visibility edge cases (records and nested types), without changing production analyzer/code-fix logic.

Changes:

  • Added a diagnostic + code-fix test for public record types (record classes).
  • Added a no-diagnostic test for public record struct (value-type records are not TypeKind.Class).
  • Added nested-type scenarios validating behavior with resultant visibility (nested public inside internal outer) and ensuring both outer/inner types are handled (including nested inside an existing [TestClass]).
Show a summary per file
File Description
test/UnitTests/MSTest.Analyzers.UnitTests/PublicTypeShouldBeTestClassAnalyzerTests.cs Adds 5 new MSTEST0004 unit tests covering record/record-struct and nested-type visibility/code-fix scenarios.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@Evangelink Evangelink marked this pull request as ready for review June 9, 2026 06:49
@Evangelink

Copy link
Copy Markdown
Member Author

🧪 Test quality grade — PR #8941

5 new test methods graded across 1 file (PublicTypeShouldBeTestClassAnalyzerTests.cs). All tests use the VerifyCS.VerifyCodeFixAsync Roslyn-testing pattern, which asserts both diagnostic locations (via [|...|] markers) and exact code-fix transforms — a rich assertion that covers the full analyzer contract. Every test is focused on a single scenario with a clear explanatory comment. No issues identified.

Test Grade Band Notes
MSTest.Analyzers.UnitTests.PublicTypeShouldBeTestClassAnalyzerTests.WhenPublicClassNestedInsideInternalClass_NoDiagnostic (new) A 90–100 No issues found.
MSTest.Analyzers.UnitTests.PublicTypeShouldBeTestClassAnalyzerTests.WhenPublicClassNestedInsidePublicNonTestClass_Diagnostic (new) A 90–100 Asserts two diagnostic locations and exact code-fix transform for both outer and inner class.
MSTest.Analyzers.UnitTests.PublicTypeShouldBeTestClassAnalyzerTests.WhenPublicClassNestedInsideTestClass_Diagnostic (new) A 90–100 Precisely marks only the un-annotated inner class; verifies the outer [TestClass] is left untouched.
MSTest.Analyzers.UnitTests.PublicTypeShouldBeTestClassAnalyzerTests.WhenPublicRecordClass_Diagnostic (new) A 90–100 Clear AAA; asserts diagnostic location and exact code-fix transform for record class.
MSTest.Analyzers.UnitTests.PublicTypeShouldBeTestClassAnalyzerTests.WhenPublicRecordStruct_NoDiagnostic (new) A 90–100 No issues found.

This advisory comment was generated automatically. Grades are heuristic and informational — they do not block merging. Re-run with /grade-tests.

Generated by Grade Tests on PR (on open / sync) for issue #8941 · sonnet46 1.3M ·

@Evangelink Evangelink merged commit 2b9bd73 into main Jun 9, 2026
44 checks passed
@Evangelink Evangelink deleted the test-assist/public-type-test-class-edge-cases-6e0b8ff7f98c3d34 branch June 9, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/automation Created or maintained by an agentic workflow. type/test-gap Missing or insufficient tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants