Skip to content

Prevent UITestMethod false failures when test invocation hops execution context - #7746

Closed
Evangelink with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-uitestmethod-broken
Closed

Prevent UITestMethod false failures when test invocation hops execution context#7746
Evangelink with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-uitestmethod-broken

Conversation

Copilot AI commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

UITestMethod could report failures even when UI assertions succeeded because TestMethodInfo.TestContext relied on TestContext.Current, which can be lost when attributes invoke ITestMethod.InvokeAsync on dispatcher/unsafe-thread paths. This manifested as ApplicationStateGuard.Unreachable() from TestMethodInfo during UI test execution.

  • Execution path fix (adapter)

    • Introduced a context-bound ITestMethod wrapper in TestMethodRunner.
    • ExecuteAsync now passes this wrapper to Executor.ExecuteAsync(...) instead of passing TestMethodInfo directly.
    • The wrapper re-establishes the per-test TestContext at the exact InvokeAsync call site, so attribute-driven cross-thread execution still resolves test context correctly.
  • Regression coverage

    • Added a focused unit test in TestMethodRunnerTests that simulates execution-context loss via ThreadPool.UnsafeQueueUserWorkItem.
    • The test uses a custom TestMethodAttribute that invokes ITestMethod.InvokeAsync from that unsafe thread path and verifies the run completes as Passed.
// Before
tcs.SetResult(await _testMethodInfo.Executor.ExecuteAsync(testMethodInfo).ConfigureAwait(false));

// After
var contextBoundTestMethod = new ContextBoundTestMethod(testMethodInfo, _testContext);
tcs.SetResult(await _testMethodInfo.Executor.ExecuteAsync(contextBoundTestMethod).ConfigureAwait(false));

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 build test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj -v minimal (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj -v minimal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet build test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj -v minimal (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj -v minimal (dns block)

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

Copilot AI requested review from Copilot and removed request for Copilot April 17, 2026 10:20
Copilot AI linked an issue Apr 17, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot April 17, 2026 10:28
Copilot AI requested review from Copilot and removed request for Copilot April 17, 2026 10:30
Copilot AI changed the title [WIP] Fix UITestMethod failure issue in test execution Prevent UITestMethod false failures when test invocation hops execution context Apr 17, 2026
Copilot AI requested a review from Evangelink April 17, 2026 10:31
@Youssef1313

Copy link
Copy Markdown
Member

Based on Copilot investigation, here is a possibly simpler fix: #7749

@Evangelink Evangelink closed this Apr 20, 2026
@Youssef1313
Youssef1313 deleted the copilot/fix-uitestmethod-broken branch April 20, 2026 16:35
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.

UITestMethod broken

3 participants