private void RunCleanup( ITestExecutionRecorder testExecutionRecorder, UnitTestRunner testRunner) { // All cleanups (Class and Assembly) run at the end of test execution. Failures in these cleanup // methods will be reported as Warnings. PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executing cleanup methods."); var cleanupResult = testRunner.RunCleanup(); PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executed cleanup methods."); if (cleanupResult != null) { // Do not attach the standard output and error messages to any test result. It is not // guaranteed that a test method of same class would have run last. We will end up // adding stdout to test method of another class. this.LogCleanupResult(testExecutionRecorder, cleanupResult); } }
private void ExecuteTestsWithTestRunner( IEnumerable <TestCase> tests, IRunContext runContext, ITestExecutionRecorder testExecutionRecorder, string source, UnitTestRunner testRunner) { TestCase test = null; UnitTestResult[] unitTestResult = null; var startTime = DateTimeOffset.MinValue; var endTime = DateTimeOffset.MinValue; bool filterHasError; var filterExpression = this.TestMethodFilter.GetFilterExpression( runContext, testExecutionRecorder, out filterHasError); if (!filterHasError) { foreach (var currentTest in tests) { // Skip test if not fitting filter criteria. if (filterExpression != null && filterExpression.MatchTestCase( currentTest, (p) => this.TestMethodFilter.PropertyValueProvider(currentTest, p)) == false) { continue; } // Send previous test result. this.SendTestResults(test, unitTestResult, startTime, endTime, testExecutionRecorder); if (this.cancellationToken != null && this.cancellationToken.Canceled) { break; } var unitTestElement = currentTest.ToUnitTestElement(source); testExecutionRecorder.RecordStart(currentTest); startTime = DateTimeOffset.Now; PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo( "Executing test {0}", unitTestElement.TestMethod.Name); if (unitTestElement.Ignored) { unitTestResult = new[] { new UnitTestResult(UnitTestOutcome.Ignored, null) }; } else { // this is done so that appropriate values of testcontext properties are set at source level // and are merged with session level parameters var sourceLevelParameters = PlatformServiceProvider.Instance.SettingsProvider.GetProperties(source); if (this.sessionParameters != null && this.sessionParameters.Count > 0) { sourceLevelParameters = sourceLevelParameters.Concat(this.sessionParameters).ToDictionary(x => x.Key, x => x.Value); } unitTestResult = testRunner.RunSingleTest(unitTestElement.TestMethod, sourceLevelParameters); } PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo( "Executed test {0}", unitTestElement.TestMethod.Name); endTime = DateTimeOffset.Now; test = currentTest; } } IList <string> warnings = null; try { PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executing cleanup methods."); var cleanupResult = testRunner.RunCleanup(); PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executed cleanup methods."); if (cleanupResult != null) { warnings = cleanupResult.Warnings; if (unitTestResult?.Length > 0) { var lastResult = unitTestResult[unitTestResult.Length - 1]; lastResult.StandardOut += cleanupResult.StandardOut; lastResult.StandardError += cleanupResult.StandardError; lastResult.DebugTrace += cleanupResult.DebugTrace; } } } finally { // Send last test result this.SendTestResults(test, unitTestResult, startTime, endTime, testExecutionRecorder); } this.LogWarnings(testExecutionRecorder, warnings); }