Generate the TestContext property for record test classes - #1866
Merged
Conversation
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.
This was referenced Aug 26, 2026
This was referenced Aug 27, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A record is a class, so it is a legal MSTest test class, but
IsSyntaxEligibleForGenerationonly matchedClassDeclarationSyntax. A[UsesVerify] [TestClass] partial recordcompiled and then failed at run time with the misleading:pointing at an attribute that was already there.
Records are now eligible. Two things follow from that:
partial class— which would have been a CS0261 kind mismatch against the record.GetParentClassesalso accepts arecord structparent. Its kind is two tokens (Keywordis only therecordhalf, withstructinClassOrStructKeyword), which the newGetPartialKeywordhandles.Record structs stay ineligible as targets:
[UsesVerify]is declared forAttributeTargets.Assembly | AttributeTargets.Class, so putting it on one is CS0592 in the consuming source regardless of what the generator does.HasAttributeOnRecordStructpins that as generating nothing.Tests on both sides.
Verify.MSTest.SourceGenerator.TestsgainsRecordTests— record, explicitrecord class, record struct, and a class nested insiderecord→record structparents; that harness compiles the generated output and asserts zero diagnostics, so a kind mismatch would fail it.Verify.MSTest.Testsgains an actualpartial recordtest class as the end-to-end proof: it throws "TestContext is null" onmain.Verify.MSTest.SourceGenerator.Tests(23) andVerify.MSTest.Tests(157 across net11.0 and net48) pass.