[test-improver] test: add edge case tests for PublicTypeShouldBeTestClassAnalyzer (MSTEST0004)#8941
Conversation
…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>
There was a problem hiding this comment.
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 recordtypes (record classes). - Added a no-diagnostic test for
public record struct(value-type records are notTypeKind.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
🧪 Test quality grade — PR #89415 new test methods graded across 1 file (
This advisory comment was generated automatically. Grades are heuristic and informational — they do not block merging. Re-run with
|
🤖 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:public record(record class) — records haveTypeKind.Classin Roslyn; the analyzer should flag them just like plain classes, but there was no test confirming this.public record struct— value-type records haveTypeKind.Struct; the analyzer explicitly skips non-Class types, so these should never be flagged.internalouter class —GetResultantVisibility()returnsInternalhere, so the rule does not apply. Without a test, a future change to the visibility check could silently start flagging these.[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.[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
[TestMethod]test cases toPublicTypeShouldBeTestClassAnalyzerTests.VerifyCS.VerifyCodeFixAsync(following the existing pattern) to assert both the diagnostic location and the code-fix output.Test Status
✅ All 13 tests pass (
MSTest.Analyzers.UnitTests, net8.0, Debug — 8 original + 5 new).Reproducibility
Trade-offs
GetResultantVisibility()that would otherwise be implicit and easy to break in a refactor.Add this agentic workflows to your repo
To install this agentic workflow, run