Fix XUnitWrapperGenerator: testArguments out of scope in ConditionalTest else branch for MemberData theories#126505
Conversation
…ta with conditional attributes
When a Theory test uses MemberDataAttribute and is also decorated with conditional
skip attributes (SkipOnPlatform, SkipOnCoreClr, etc.), the generator wraps the
MemberDataTest inside a ConditionalTest. The ConditionalTest.GenerateTestExecution()
puts the foreach loop (from MemberDataTest) inside the if-condition block, but the
else block uses MemberDataTest.TestNameExpression which was previously copied from
the inner BasicTestMethod's TestNameExpression. That expression contains a reference
to the testArguments loop variable (e.g., testArguments[0], string.Join(",", testArguments)),
causing CS0103 since testArguments is only in scope inside the foreach loop.
Fix: Change MemberDataTest.TestNameExpression to use a static literal string
"alias::Type.Method(...)" that doesn't reference any loop variables. The inner
BasicTestMethod.TestNameExpression still references testArguments for per-iteration
reporting inside the foreach loop.
Fixes: error CS0103: The name 'testArguments' does not exist in the current context
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/ff0bc935-d962-4daf-891b-5592aab629f4
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
testArguments out of scope in ConditionalTest else branch for MemberData theories
|
Tagging subscribers to this area: @agocke |
There was a problem hiding this comment.
Pull request overview
Fixes a C# compile error in the XUnitWrapperGenerator output when [Theory] + [MemberData] tests are wrapped in a ConditionalTest (e.g., via [SkipOnPlatform]), by ensuring the else-branch skip-reporting name does not reference the foreach loop variable (testArguments) that’s only in scope in the if branch.
Changes:
- Updates
MemberDataTestto use a staticTestNameExpressionliteral (no loop variable reference). - Reorders
MemberDataTestconstructor assignments soMethod/ContainingTypeare set before building the test name. - Adds an explanatory comment about the scope issue (ConditionalTest else branch vs foreach scope).
|
build analysis stuck |
🤖 Copilot Code Review — PR #126505Note This review was generated by Copilot using multi-model analysis (Claude Opus 4.6 primary, Claude Sonnet 4.6 + GPT-5.3-codex secondary). Holistic AssessmentMotivation: The bug is real and clear. When Approach: The fix is minimal and correct. Changing Summary: ✅ LGTM. The fix correctly resolves a compile-time error in generated code with a focused 3-line change. All code paths were traced and verified correct. No new issues introduced. All three models (Claude Opus 4.6, Claude Sonnet 4.6, GPT-5.3-codex) agree on the verdict. Detailed Findings✅ Correctness — Fix resolves the CS0103 scope errorTraced the full code-generation flow:
✅ Property assignment reordering is correctMoving ✅ Equality/hashing behavior preserved (flagged by all 3 models)
💡 Minor test-name fidelity change in skip reporting — acceptable (flagged by all 3 models)When a 💡 No regression test — acceptable for this contextThe XUnitWrapperGenerator has no dedicated unit test suite. The "test" is whether projects using 💡 Pre-existing:
|
When a
[Theory]with[MemberData]is also annotated with a conditional skip attribute ([SkipOnPlatform],[SkipOnCoreClr], etc.), the generator wraps theMemberDataTestinside aConditionalTest. Theelsebranch of that conditional reports the skipped test usingMemberDataTest.TestNameExpression— which was previously copied verbatim from the innerBasicTestMethod.TestNameExpression. That expression references thetestArgumentsforeach loop variable, which is only in scope inside theifbranch, producing:Fix (
ITestInfo.cs—MemberDataTestconstructor):Method/ContainingTypeare set firstTestNameExpression = innerTest.TestNameExpressionwith a static literal:$"\"{externAlias}::{ContainingType}.{Method}(...)\""BasicTestMethod.TestNameExpression(used inside the foreach loop for per-iteration filter/reporting) is unchanged