/// <summary> /// Executes the specified test case /// </summary> /// <param name="vm">Test explorer view model</param> /// <param name="testToRun">Test that hosts the test case</param> /// <param name="caseToRun">Test case to run</param> /// <param name="token">Token to cancel tests</param> private async Task ExecuteCaseAsync(TestExplorerToolWindowViewModel vm, TestItem testToRun, TestCaseItem caseToRun, CancellationToken token) { if (token.IsCancellationRequested) { return; } var timeout = testToRun.Plan.TimeoutValue; caseToRun.Log("Test set execution started" + (timeout == 0 ? "" : $" with {timeout}ms timeout")); var watch = new Stopwatch(); watch.Start(); try { // --- Set the test case machine context caseToRun.Plan.MachineContext = this; // --- Execute arrange ExecuteArrange(caseToRun.Plan, testToRun.Plan.ArrangeAssignments); ReportTimeDetail("Arrange:", caseToRun, watch); // --- Execute the test code var success = await InvokeCodeAsync(caseToRun, testToRun.Plan.Act, timeout, token, watch); ReportTimeDetail("Act:", caseToRun, watch); if (success) { // --- Execute assertions if (ExecuteAssert(caseToRun.Plan, testToRun.Plan.Assertions, out var stopIndex)) { caseToRun.State = TestState.Success; } else { caseToRun.State = TestState.Failed; caseToRun.Log($"Assertion #{stopIndex} failed.", LogEntryType.Fail); } ReportTimeDetail("Assert:", caseToRun, watch); } } catch (Exception ex) { HandleException(caseToRun, ex); } finally { vm.UpdateCounters(); caseToRun.Log($"Test execution completed in {watch.Elapsed.TotalSeconds:####0.####} seconds"); ReportTestResult(caseToRun); } }