Skip to content

Generate the TestContext property for record test classes - #1866

Merged
SimonCropp merged 1 commit into
mainfrom
fix-mstest-generator-records
Aug 17, 2026
Merged

Generate the TestContext property for record test classes#1866
SimonCropp merged 1 commit into
mainfrom
fix-mstest-generator-records

Conversation

@SimonCropp

Copy link
Copy Markdown
Member

A record is a class, so it is a legal MSTest test class, but IsSyntaxEligibleForGeneration only matched ClassDeclarationSyntax. A [UsesVerify] [TestClass] partial record compiled and then failed at run time with the misleading:

System.Exception: TestContext is null. Ensure test class has a `[UsesVerify]` attribute (or inherits from `VerifyBase`).

pointing at an attribute that was already there.

Records are now eligible. Two things follow from that:

  • A partial declaration has to repeat the kind of the type, so the emitter takes the keyword from the declaration instead of always writing partial class — which would have been a CS0261 kind mismatch against the record.
  • The same applies to every enclosing type, so GetParentClasses also accepts a record struct parent. Its kind is two tokens (Keyword is only the record half, with struct in ClassOrStructKeyword), which the new GetPartialKeyword handles.

Record structs stay ineligible as targets: [UsesVerify] is declared for AttributeTargets.Assembly | AttributeTargets.Class, so putting it on one is CS0592 in the consuming source regardless of what the generator does. HasAttributeOnRecordStruct pins that as generating nothing.

Tests on both sides. Verify.MSTest.SourceGenerator.Tests gains RecordTests — record, explicit record class, record struct, and a class nested inside recordrecord struct parents; that harness compiles the generated output and asserts zero diagnostics, so a kind mismatch would fail it. Verify.MSTest.Tests gains an actual partial record test class as the end-to-end proof: it throws "TestContext is null" on main.

Verify.MSTest.SourceGenerator.Tests (23) and Verify.MSTest.Tests (157 across net11.0 and net48) pass.

A record is a class, so it is a legal MSTest test class, but only
ClassDeclarationSyntax passed the syntax filter. A [UsesVerify] [TestClass]
partial record compiled and then failed at run time with the misleading
"TestContext is null. Ensure test class has a `[UsesVerify]` attribute".

Records are now eligible, and since a partial declaration has to repeat the kind
of the type, the emitter takes the keyword from the declaration rather than
always writing `class`. The same applies to the enclosing types, so
GetParentClasses also handles a record struct parent, whose kind is two tokens.

Record structs stay ineligible as targets: [UsesVerify] is only valid on a class.
@SimonCropp SimonCropp added this to the 32.0.0 milestone Aug 17, 2026
@SimonCropp
SimonCropp merged commit d3a9e7c into main Aug 17, 2026
7 checks passed
@SimonCropp
SimonCropp deleted the fix-mstest-generator-records branch August 17, 2026 12:44
This was referenced Aug 26, 2026
This was referenced Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant