public async Task TestRunManager_PublishTestRun() { var logger = new Mock <ITraceLogger>(); var publisher = new Mock <ITestRunPublisher>(); var telemetry = new Mock <ITelemetryDataCollector>(); var runManager = new TestRunManager(publisher.Object, logger.Object, telemetry.Object); var fakeRun = new TestRun("mocha/1", "somename", 1) { TestRunSummary = new TestRunSummary { TotalPassed = 5, TotalSkipped = 1, TotalFailed = 1, TotalExecutionTime = TimeSpan.FromMinutes(1), TotalTests = 7 } }; publisher.Setup(x => x.PublishAsync(It.IsAny <TestRun>())).Returns(Task.FromResult(GetFakePipelineTestRun(fakeRun, 1))); await runManager.PublishAsync(fakeRun); publisher.Verify(x => x.PublishAsync(It.IsAny <TestRun>())); }
/// <summary> /// Publishes the run and resets the parser by resetting the state context and current state /// </summary> private void AttemptPublishAndResetParser() { Logger.Info($"JestTestResultParser : Resetting the parser and attempting to publish the test run at line {_stateContext.CurrentLineNumber}."); var testRunToPublish = _stateContext.TestRun; // We have encountered passed test cases but no passed summary was encountered if (testRunToPublish.PassedTests.Count != 0 && testRunToPublish.TestRunSummary.TotalPassed == 0) { Logger.Error("JestTestResultParser : Passed tests were encountered but no passed summary was encountered."); Telemetry.AddAndAggregate(JestTelemetryConstants.PassedTestCasesFoundButNoPassedSummary, new List <int> { _stateContext.TestRun.TestRunId }, JestTelemetryConstants.EventArea); } else if (_stateContext.VerboseOptionEnabled && testRunToPublish.TestRunSummary.TotalPassed != testRunToPublish.PassedTests.Count) { // If encountered failed tests does not match summary fire telemetry Logger.Error($"JestTestResultParser : Passed tests count does not match passed summary" + $" at line {_stateContext.CurrentLineNumber}"); Telemetry.AddAndAggregate(JestTelemetryConstants.PassedSummaryMismatch, new List <int> { testRunToPublish.TestRunId }, JestTelemetryConstants.EventArea); } // We have encountered failed test cases but no failed summary was encountered if (testRunToPublish.FailedTests.Count != 0 && testRunToPublish.TestRunSummary.TotalFailed == 0) { Logger.Error("JestTestResultParser : Failed tests were encountered but no failed summary was encountered."); Telemetry.AddAndAggregate(JestTelemetryConstants.FailedTestCasesFoundButNoFailedSummary, new List <int> { _stateContext.TestRun.TestRunId }, JestTelemetryConstants.EventArea); } else if (testRunToPublish.TestRunSummary.TotalFailed != testRunToPublish.FailedTests.Count) { // If encountered failed tests does not match summary fire telemtry Logger.Error($"JestTestResultParser : Failed tests count does not match failed summary" + $" at line {_stateContext.CurrentLineNumber}"); Telemetry.AddAndAggregate(JestTelemetryConstants.FailedSummaryMismatch, new List <int> { testRunToPublish.TestRunId }, JestTelemetryConstants.EventArea); } // Ensure some summary data was detected before attempting a publish, ie. check if the state is not test results state switch (_currentState) { case JestParserStates.ExpectingTestRunStart: Logger.Error("JestTestResultParser : Skipping publish as no test cases or summary has been encountered."); break; case JestParserStates.ExpectingTestResults: case JestParserStates.ExpectingStackTraces: if (testRunToPublish.PassedTests.Count != 0 || testRunToPublish.FailedTests.Count != 0 || testRunToPublish.SkippedTests.Count != 0) { Logger.Error("JestTestResultParser : Skipping publish as testcases were encountered but no summary was encountered."); Telemetry.AddAndAggregate(JestTelemetryConstants.TestCasesFoundButNoSummary, new List <int> { _stateContext.TestRun.TestRunId }, JestTelemetryConstants.EventArea); } break; case JestParserStates.ExpectingTestRunSummary: if (testRunToPublish.TestRunSummary.TotalTests == 0) { Logger.Error("JestTestResultParser : Skipping publish as total tests was 0."); Telemetry.AddAndAggregate(JestTelemetryConstants.TotalTestsZero, new List <int> { _stateContext.TestRun.TestRunId }, JestTelemetryConstants.EventArea); break; } if (testRunToPublish.TestRunSummary.TotalExecutionTime.TotalMilliseconds == 0) { Logger.Error("JestTestResultParser : Total test run time was 0 or not encountered."); Telemetry.AddAndAggregate(JestTelemetryConstants.TotalTestRunTimeZero, new List <int> { _stateContext.TestRun.TestRunId }, JestTelemetryConstants.EventArea); } // Trim the stack traces of extra newlines etc. foreach (var failedTest in testRunToPublish.FailedTests) { if (failedTest.StackTrace != null) { failedTest.StackTrace = failedTest.StackTrace.TrimEnd(); } } // Only publish if total tests was not zero TestRunManager.PublishAsync(testRunToPublish); break; } ResetParser(); }
/// <summary> /// Publishes the run and resets the parser by resetting the state context and current state /// </summary> private void AttemptPublishAndResetParser() { Logger.Info($"MochaTestResultParser : Resetting the parser and attempting to publish the test run at line {_stateContext.CurrentLineNumber}."); var testRunToPublish = _stateContext.TestRun; // We have encountered failed test cases but no failed summary was encountered if (testRunToPublish.FailedTests.Count != 0 && testRunToPublish.TestRunSummary.TotalFailed == 0) { Logger.Error("MochaTestResultParser : Failed tests were encountered but no failed summary was encountered."); Telemetry.AddAndAggregate(MochaTelemetryConstants.FailedTestCasesFoundButNoFailedSummary, new List <int> { _stateContext.TestRun.TestRunId }, MochaTelemetryConstants.EventArea); } else if (testRunToPublish.TestRunSummary.TotalFailed != testRunToPublish.FailedTests.Count) { // If encountered failed tests does not match summary fire telemetry Logger.Error($"MochaTestResultParser : Failed tests count does not match failed summary" + $" at line {_stateContext.CurrentLineNumber}"); Telemetry.AddAndAggregate(MochaTelemetryConstants.FailedSummaryMismatch, new List <int> { testRunToPublish.TestRunId }, MochaTelemetryConstants.EventArea); } // We have encountered pending test cases but no pending summary was encountered if (testRunToPublish.SkippedTests.Count != 0 && testRunToPublish.TestRunSummary.TotalSkipped == 0) { Logger.Error("MochaTestResultParser : Skipped tests were encountered but no skipped summary was encountered."); Telemetry.AddAndAggregate(MochaTelemetryConstants.PendingTestCasesFoundButNoFailedSummary, new List <int> { _stateContext.TestRun.TestRunId }, MochaTelemetryConstants.EventArea); } else if (testRunToPublish.TestRunSummary.TotalSkipped != testRunToPublish.SkippedTests.Count) { // If encountered skipped tests does not match summary fire telemetry Logger.Error($"MochaTestResultParser : Pending tests count does not match pending summary" + $" at line {_stateContext.CurrentLineNumber}"); Telemetry.AddAndAggregate(MochaTelemetryConstants.PendingSummaryMismatch, new List <int> { testRunToPublish.TestRunId }, MochaTelemetryConstants.EventArea); } // Ensure some summary data was detected before attempting a publish, ie. check if the state is not test results state switch (_currentState) { case MochaParserStates.ExpectingTestResults: if (testRunToPublish.PassedTests.Count != 0 || testRunToPublish.FailedTests.Count != 0 || testRunToPublish.SkippedTests.Count != 0) { Logger.Error("MochaTestResultParser : Skipping publish as testcases were encountered but no summary was encountered."); Telemetry.AddAndAggregate(MochaTelemetryConstants.PassedTestCasesFoundButNoPassedSummary, new List <int> { _stateContext.TestRun.TestRunId }, MochaTelemetryConstants.EventArea); } break; default: // Publish the test run if reset and publish was called from any state other than the test results state // Calculate total tests testRunToPublish.TestRunSummary.TotalTests = testRunToPublish.TestRunSummary.TotalPassed + testRunToPublish.TestRunSummary.TotalFailed + testRunToPublish.TestRunSummary.TotalSkipped; // Trim the stack traces of extra newlines etc. foreach (var failedTest in testRunToPublish.FailedTests) { if (failedTest.StackTrace != null) { failedTest.StackTrace = failedTest.StackTrace.TrimEnd(); } } TestRunManager.PublishAsync(testRunToPublish); break; } ResetParser(); }