Skip to content

Fix WinUI TestContext.Current being null#7749

Merged
Evangelink merged 8 commits into
mainfrom
dev/ygerges/fix-winui
Apr 21, 2026
Merged

Fix WinUI TestContext.Current being null#7749
Evangelink merged 8 commits into
mainfrom
dev/ygerges/fix-winui

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a WinUI execution scenario where the ambient TestContext.Current can be null during test execution by ensuring the ITestContext is available directly on TestMethodInfo when invoking the executor.

Changes:

  • Assigns the runner’s _testContext onto the TestMethodInfo instance immediately before executing the test method.
  • Reworks TestMethodInfo.TestContext from a static accessor into an instance property that can use an explicitly assigned context, with a fallback to TestContext.Current.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs Sets testMethodInfo.TestContext before invoking the TestMethodAttribute executor.
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodInfo.cs Converts TestContext to an instance property backed by a stored context, falling back to TestContext.Current.

@Evangelink Evangelink left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the fix looks correct and well-targeted. The approach of using an instance field with AsyncLocal fallback is clean and maintains backward compatibility for non-WinUI scenarios.

A few observations:

  1. Missing tests: There are no unit tests covering the new behavior. A test in TestMethodRunnerTests that verifies TestContext is properly set on the TestMethodInfo before executor invocation would help prevent regression. Specifically, a test showing the fallback works when TestContext.Current is null but field is set would directly validate the WinUI fix.

  2. PR description: The body is currently empty (template placeholder). It would help reviewers to add root cause context — specifically that AsyncLocal doesn't flow through WinUI's DispatcherQueue.TryEnqueue, causing TestContext.Current to be null on the dispatcher thread.

  3. Minor: The field is never cleared after execution, but since TestMethodInfo is created fresh per test (via TypeCache.ResolveTestMethodInfo), there's no stale state risk. No action needed.

Looks good modulo tests and documentation. 🎉 Temperature: warm/supportive. Sentiment: approving with minor suggestions.

@Youssef1313

Copy link
Copy Markdown
Member Author

@Evangelink I'll let you complete this by copying the tests that Copilot originally wrote in the other PR (please ensure the tests will fail on current main).

@Evangelink

Copy link
Copy Markdown
Member

@copilot fix these issues:

Check failure on line 195 in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs

@azure-pipelines
azure-pipelines
/ microsoft.testfx (Build Windows Debug)
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs#L195

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs(195,1): error : [TestMethodInfoInvokeShouldWaitForAsyncTestMethodsToComplete] [net462] This program location is thought to be unreachable. File='/_/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodInfo.cs' Line=58
Check failure on line 214 in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs

@azure-pipelines
azure-pipelines
/ microsoft.testfx (Build Windows Debug)
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs#L214

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs(214,1): error : [TestMethodInfoInvokeAsyncShouldHandleThrowAssertInconclusive] [net462] This program location is thought to be unreachable. File='/_/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodInfo.cs' Line=58
Check failure on line 234 in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs

@azure-pipelines
azure-pipelines
/ microsoft.testfx (Build Windows Debug)
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs#L234

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs(234,1): error : [TestMethodInfoInvokeAsyncShouldHandleAssertInconclusive] [net462] This program location is thought to be unreachable. File='/_/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodInfo.cs' Line=58
Check failure on line 252 in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs

@azure-pipelines
azure-pipelines
/ microsoft.testfx (Build Windows Debug)
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs#L252

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs(252,1): error : [TestMethodInfoInvokeShouldHandleThrowAssertInconclusive] [net462] This program location is thought to be unreachable. File='/_/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodInfo.cs' Line=58
Check failure on line 272 in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs

@azure-pipelines
azure-pipelines
/ microsoft.testfx (Build Windows Debug)
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs#L272

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs(272,1): error : [TestMethodInfoInvokeShouldHandleAssertInconclusive] [net462] This program location is thought to be unreachable. File='/_/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodInfo.cs' Line=58
View more details on Azure Pipelines

Copilot AI review requested due to automatic review settings April 17, 2026 18:02
@Youssef1313
Youssef1313 removed the request for review from Copilot April 17, 2026 18:02

Copilot AI commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

@copilot fix these issues:

[Check failure on line 195 in test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs](#7749...

Fixed in ff33e06. I restored the TestMethodInfo.TestContext fallback to TestContext.Current while keeping the runner-set context path, which resolves the net462 unreachable failures you linked.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • c50vsblobprodcus330.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet test test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj --filter TestMethodInfoInvokeShouldWaitForAsyncTestMethodsToComplete|TestMethodInfoInvokeAsyncShouldHandleThrowAssertInconclusive|TestMethodInfoInvokeAsyncShouldHandleAssertInconclusive|TestMethodInfoInvokeShouldHandleThrowAssertInconclusive|TestMethodInfoInvokeSh (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet test test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj --filter TestMethodInfoInvokeShouldWaitForAsyncTestMethodsToComplete|TestMethodInfoInvokeAsyncShouldHandleThrowAssertInconclusive|TestMethodInfoInvokeAsyncShouldHandleAssertInconclusive|TestMethodInfoInvokeShouldHandleThrowAssertInconclusive|TestMethodInfoInvokeSh (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from Evangelink April 17, 2026 18:03
Copilot AI review requested due to automatic review settings April 20, 2026 10:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

@Evangelink
Evangelink marked this pull request as ready for review April 20, 2026 11:43
Copilot AI and others added 2 commits April 20, 2026 11:49
Copilot AI review requested due to automatic review settings April 20, 2026 11:51
@Youssef1313
Youssef1313 removed the request for review from Copilot April 20, 2026 11:51

Copilot AI commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • c50vsblobprodcus330.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/testfx/testfx/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj --filter FullyQualifiedName~RunTestMethodShouldPassWhenAttributeInvokesTestMethodOnExecutionContextUnsafeThread --nologo (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/testfx/testfx/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj --filter FullyQualifiedName~RunTestMethodShouldPassWhenAttributeInvokesTestMethodOnExecutionContextUnsafeThread --nologo (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Evangelink

Copy link
Copy Markdown
Member

@copilot fix

azure-pipelines
/ microsoft.testfx (Build Windows Debug)
test\UnitTests\MSTestAdapter.PlatformServices.UnitTests\Execution\TestMethodRunnerTests.cs#L466

test\UnitTests\MSTestAdapter.PlatformServices.UnitTests\Execution\TestMethodRunnerTests.cs(466,53): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'Task' does not contain a definition for 'WaitAsync' and no accessible extension method 'WaitAsync' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?)

Copilot AI review requested due to automatic review settings April 20, 2026 16:29
@Youssef1313
Youssef1313 removed the request for review from Copilot April 20, 2026 16:29

Copilot AI commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

@copilot fix

azure-pipelines
...

Fixed in d206a41: replaced WaitAsync with a Task.WhenAny timeout path so this compiles on net462 while still failing deterministically on hangs. No UI changes to screenshot for this update.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • c50vsblobprodcus330.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/testfx/testfx/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj --filter FullyQualifiedName~RunTestMethodShouldPassWhenAttributeInvokesTestMethodOnExecutionContextUnsafeThread --nologo (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/testfx/testfx/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj --filter FullyQualifiedName~RunTestMethodShouldPassWhenAttributeInvokesTestMethodOnExecutionContextUnsafeThread --nologo (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI review requested due to automatic review settings April 21, 2026 08:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new

@Evangelink
Evangelink enabled auto-merge April 21, 2026 09:17
@Evangelink
Evangelink merged commit c46d03b into main Apr 21, 2026
14 checks passed
@Evangelink
Evangelink deleted the dev/ygerges/fix-winui branch April 21, 2026 09:17
@Evangelink Evangelink mentioned this pull request Apr 24, 2026
@vberkaltun

Copy link
Copy Markdown

Thanks for fixing this issue!

Is there a plan to release a new stable version with this fix soon? It would be great if a release is planned for this week. Otherwise, I will stay on 4.1.0 for now and avoid using 4.2.1 until a version with the fix is available.

@Evangelink

Evangelink commented Apr 28, 2026

Copy link
Copy Markdown
Member

/backport to rel/4.2

1 similar comment
@Evangelink

Copy link
Copy Markdown
Member

/backport to rel/4.2

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to rel/4.2: https://github.com/microsoft/testfx/actions/runs/25057861590

@Evangelink

Copy link
Copy Markdown
Member

Thanks for fixing this issue!

Is there a plan to release a new stable version with this fix soon? It would be great if a release is planned for this week. Otherwise, I will stay on 4.1.0 for now and avoid using 4.2.1 until a version with the fix is available.

Yes sorry I am quite burried with work, I'm queueing that now

Evangelink added a commit that referenced this pull request Apr 28, 2026
…ckport to rel/4.2) (#7905)

Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
Co-authored-by: Amaury Levé <amauryleve@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@vberkaltun

Copy link
Copy Markdown

Yes sorry I am quite burried with work, I'm queueing that now

No problem, that would be great if possible. I will keep an eye on the release this week.

@Evangelink

Copy link
Copy Markdown
Member

There was an issue with the release pipeline, I'm looking at it now

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.

5 participants