diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs b/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs index 1c11cf658f..849b6ba9b1 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs @@ -496,11 +496,27 @@ private async Task ExecuteTestAsync(ITestContext executionContext, { try { + ExecutionContext? capturedContext = testMethodInfo.Parent.ExecutionContext + ?? testMethodInfo.Parent.Parent.ExecutionContext; + + // Fast path: when no ExecutionContext was captured (the common case), + // ExecutionContextHelpers.RunOnContext would simply call the action inline. + // Skip the TaskCompletionSource bridge, async-lambda closure, and Action + // delegate allocations entirely. + if (capturedContext is null) + { + using (TestContextImplementation.SetCurrentTestContext(executionContext as TestContext)) + { + testMethodInfo.TestContext = executionContext; + return await _testMethodInfo.Executor.ExecuteAsync(testMethodInfo).ConfigureAwait(false); + } + } + var tcs = new TaskCompletionSource(); #pragma warning disable VSTHRD101 // Avoid unsupported async delegates ExecutionContextHelpers.RunOnContext( - testMethodInfo.Parent.ExecutionContext ?? testMethodInfo.Parent.Parent.ExecutionContext, + capturedContext, async () => { try