/// <summary> /// Copies the result messages to unitTestResult /// </summary> /// <param name="unitTestResult">TRX TestResult</param> /// <param name="testResult"> rock steady test result</param> private static void UpdateResultMessages(TrxObjectModel.UnitTestResult unitTestResult, ObjectModel.TestResult testResult) { StringBuilder debugTrace = new StringBuilder(); StringBuilder stdErr = new StringBuilder(); StringBuilder stdOut = new StringBuilder(); foreach (Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResultMessage message in testResult.Messages) { if (ObjectModel.TestResultMessage.AdditionalInfoCategory.Equals(message.Category, StringComparison.OrdinalIgnoreCase)) { unitTestResult.AddTextMessage(message.Text); } else if (ObjectModel.TestResultMessage.DebugTraceCategory.Equals(message.Category, StringComparison.OrdinalIgnoreCase)) { debugTrace.AppendLine(message.Text); } else if (ObjectModel.TestResultMessage.StandardErrorCategory.Equals(message.Category, StringComparison.OrdinalIgnoreCase)) { stdErr.AppendLine(message.Text); } else if (ObjectModel.TestResultMessage.StandardOutCategory.Equals(message.Category, StringComparison.OrdinalIgnoreCase)) { stdOut.AppendLine(message.Text); } else { ObjectModel.EqtTrace.Warning("The message category " + message.Category + " does not match any predefined category."); } } unitTestResult.DebugTrace = debugTrace.ToString(); unitTestResult.StdErr = stdErr.ToString(); unitTestResult.StdOut = stdOut.ToString(); }
/// <summary> /// Converts the rockSteady result to unit test result /// </summary> /// <param name="rockSteadyTestResult"> rock steady test result</param> /// <param name="testElement"> testElement of that test</param> /// <param name="testOutcome"> Test outcome </param> /// <param name="testRun"> test run object </param> /// <param name="trxFileDirectory"> TRX file directory</param> /// <returns> TestResult object </returns> internal static TrxObjectModel.UnitTestResult ToUnitTestResult( ObjectModel.TestResult rockSteadyTestResult, TrxObjectModel.UnitTestElement testElement, TrxObjectModel.TestOutcome testOutcome, TrxObjectModel.TestRun testRun, string trxFileDirectory) { TrxObjectModel.UnitTestResult qtoolsResult = GetQToolsTestResultFromTestResult(rockSteadyTestResult, testElement, testOutcome, testRun); // Clear exsting messages and store rocksteady result messages. qtoolsResult.TextMessages = null; UpdateResultMessages(qtoolsResult, rockSteadyTestResult); // Save result attachments to target location. UpdateTestResultAttachments(rockSteadyTestResult, qtoolsResult, testRun, trxFileDirectory, true); return(qtoolsResult); }
private static void UpdateTestResultAttachments(ObjectModel.TestResult rockSteadyTestResult, TrxObjectModel.UnitTestResult testResult, TestRun testRun, string trxFileDirectory, bool addAttachments) { if (rockSteadyTestResult.Attachments == null || rockSteadyTestResult.Attachments.Count == 0) { return; } // the testResult needs to have the testRun property set. Otherwise Data Collector entries can't be added. testResult.SetTestRun(testRun); // result files List <string> resultFiles = new List <string>(); // data collection files List <CollectorDataEntry> collectorEntries = new List <CollectorDataEntry>(); foreach (ObjectModel.AttachmentSet attachmentSet in rockSteadyTestResult.Attachments) { try { // If the attachement is from data collector if (attachmentSet.Uri.AbsoluteUri.StartsWith(TrxLogger.DataCollectorUriPrefix, StringComparison.OrdinalIgnoreCase)) { CollectorDataEntry collectorEntry = ToCollectorEntry(attachmentSet, testResult.Id.ExecutionId.Id, testRun, trxFileDirectory); collectorEntries.Add(collectorEntry); } else { IList <string> testResultFiles = ToResultFiles(attachmentSet, testResult.Id.ExecutionId.Id, testRun, trxFileDirectory); resultFiles.AddRange(testResultFiles); } } catch (Exception e) { string errorMsg = string.Format( CultureInfo.CurrentCulture, TrxLoggerResources.FailureToAttach, attachmentSet.DisplayName, e.GetType().ToString(), e.Message); StringBuilder stdErr = new StringBuilder(testResult.StdErr); stdErr.AppendLine(errorMsg); testResult.StdErr = stdErr.ToString(); testResult.Outcome = TrxObjectModel.TestOutcome.Error; } } if (addAttachments) { if (resultFiles.Count > 0) { testResult.AddResultFiles(resultFiles); } if (collectorEntries.Count > 0) { testResult.AddCollectorDataEntries(collectorEntries); } } }
/// <summary> /// Called when a test result is received. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The eventArgs. /// </param> internal void TestResultHandler(object sender, ObjectModel.Logging.TestResultEventArgs e) { if (this.testRun == null) { Guid runId = Guid.NewGuid(); this.testRun = new TestRun(runId); // We cannot rely on the StartTime for the first test result // In case of parallel, first test result is the fastest test and not the one which started first. // Setting Started to DateTime.Now in Intialize will make sure we include the startup cost, which was being ignored earlier. // This is in parity with the way we set this.testRun.Finished this.testRun.Started = this.testRunStartTime; // Save default test settings string runDeploymentRoot = FileHelper.ReplaceInvalidFileNameChars(this.testRun.Name); TestRunConfiguration testrunConfig = new TestRunConfiguration("default"); testrunConfig.RunDeploymentRootDirectory = runDeploymentRoot; this.testRun.RunConfiguration = testrunConfig; } // Convert skipped test to a log entry as that is the behaviour of mstest. if (e.Result.Outcome == ObjectModel.TestOutcome.Skipped) { this.HandleSkippedTest(e.Result); } // Create MSTest test element from rocksteady test case UnitTestElement testElement = Converter.ToUnitTestElement(e.Result); // Conver the rocksteady result to MSTest result TrxLoggerObjectModel.TestOutcome testOutcome = Converter.ToOutcome(e.Result.Outcome); TrxLoggerObjectModel.UnitTestResult testResult = Converter.ToUnitTestResult(e.Result, testElement, testOutcome, this.testRun, this.testResultsDirPath); // Set various counts (passtests, failed tests, total tests) this.totalTests++; if (testResult.Outcome == TrxLoggerObjectModel.TestOutcome.Failed) { this.testRunOutcome = TrxLoggerObjectModel.TestOutcome.Failed; this.failTests++; } else if (testResult.Outcome == TrxLoggerObjectModel.TestOutcome.Passed) { this.passTests++; } // Add results to in-memory lists that are saved to the xml at completion. this.results.Add(testResult); if (!this.testElements.Contains(testElement)) { this.testElements.Add(testElement); } // create a test entry TestEntry te = new TestEntry(testElement.Id, TestListCategory.UncategorizedResults.Id); te.ExecId = testElement.ExecutionId; this.entries.Add(te); }