From 07ca3053674ec00735165147305f2e2a0022ecdb Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Fri, 13 Mar 2026 07:44:06 +0100 Subject: [PATCH] Cleanup dead code for exception handling --- .../Execution/TestMethodRunner.cs | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs b/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs index 6f02c9a404..ef0e51441c 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs @@ -72,28 +72,19 @@ internal async Task ExecuteAsync(string? initializationLogs, strin { result = await RunTestMethodAsync().ConfigureAwait(false); } - catch (TestFailedException ex) - { - result = [new TestResult { TestFailureException = ex }]; - } catch (Exception ex) { - if (result == null || result.Length == 0) - { - result = [new TestResult { Outcome = UnitTestOutcome.Error }]; - } - -#pragma warning disable IDE0056 // Use index operator - result[result.Length - 1] = new TestResult - { - TestFailureException = new TestFailedException(UnitTestOutcome.Error, ex.TryGetMessage(), ex.TryGetStackTraceInformation()), - LogOutput = result[result.Length - 1].LogOutput, - LogError = result[result.Length - 1].LogError, - DebugTrace = result[result.Length - 1].DebugTrace, - TestContextMessages = result[result.Length - 1].TestContextMessages, - Duration = result[result.Length - 1].Duration, - }; -#pragma warning restore IDE0056 // Use index operator + // NOTE: We intentionally don't have any special casing for TestFailedException in this code path. + // It's handled down by TestMethodInfo which also unwraps TargetInvocationException. + // RunTestMethodAsync is not supposed to throw any exceptions. So it's always an **error** if we got an exception here. + result = + [ + new TestResult + { + Outcome = UnitTestOutcome.Error, + TestFailureException = new TestFailedException(UnitTestOutcome.Error, ex.TryGetMessage(), ex.TryGetStackTraceInformation()), + }, + ]; } finally {