Skip to content

MemberData analyzer: analyze test methods declared in records - #232

Merged
bradwilson merged 2 commits into
xunit:mainfrom
meziantou:feature/memberdata-record-classes-a5be15
Sep 16, 2026
Merged

bradwilson merged 2 commits into
xunit:mainfrom
meziantou:feature/memberdata-record-classes-a5be15

Conversation

@meziantou

Copy link
Copy Markdown
Contributor

Summary

All MemberData rules (xUnit1014–1021, 1036–1042, 1053, …) were silently skipped for test methods declared inside a record, record class, or record struct.

MemberDataShouldReferenceValidMember.GetClassTypesForAttribute found the containing type with FirstAncestorOrSelf<ClassDeclarationSyntax>(). Records are RecordDeclarationSyntax, which does not derive from ClassDeclarationSyntax, so the lookup returned null and the analyzer returned early.

Repro

using Xunit;

public record TestClass {
    [Theory]
    [MemberData("Missing")]
    public void Test(int _) { }
}
  • Expected: xUnit1015
  • Before this PR: no diagnostics

Fix

The lookup now uses FirstAncestorOrSelf<TypeDeclarationSyntax>(), which matches classes, records, and structs. The two code fixers that call this helper (ExtraValueFixer, NullShouldNotBeUsedForIncompatibleParameterFixer) get the fix too.

Tests

  • Added record, record class, and record struct scenarios to the existing X1015_MemberDataShouldReferenceValidMemberTests.V2_and_V3 test, and raised its language version from C# 8 to C# 10 so record struct compiles.
  • Checked that the new scenarios fail without the fix (0 diagnostics returned, 3 expected).
  • xunit.analyzers.tests and xunit.analyzers.latest.tests pass on net8.0. The net472 tests were not run because this was tested on macOS.

meziantou and others added 2 commits September 13, 2026 21:26
GetClassTypesForAttribute looked for a ClassDeclarationSyntax ancestor, so
test methods inside record / record class / record struct types (which are
RecordDeclarationSyntax) were silently skipped by all MemberData rules.
Use TypeDeclarationSyntax instead.
@bradwilson
bradwilson merged commit 5e2019d into xunit:main Sep 16, 2026
1 check passed
@bradwilson

Copy link
Copy Markdown
Member

Thanks!

@bradwilson

Copy link
Copy Markdown
Member

Available in 2.2.0-pre.14 https://xunit.net/docs/using-ci-builds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants