Esempio n. 1
0
        public static TrxObjectModel.TestOutcome ToOutcome(ObjectModel.TestOutcome rockSteadyOutcome)
        {
            TrxObjectModel.TestOutcome outcome = TrxObjectModel.TestOutcome.Failed;

            switch (rockSteadyOutcome)
            {
            case ObjectModel.TestOutcome.Failed:
                outcome = TrxObjectModel.TestOutcome.Failed;
                break;

            case ObjectModel.TestOutcome.Passed:
                outcome = TrxObjectModel.TestOutcome.Passed;
                break;

            case ObjectModel.TestOutcome.Skipped:
            case ObjectModel.TestOutcome.None:
            case ObjectModel.TestOutcome.NotFound:
                outcome = TrxObjectModel.TestOutcome.NotExecuted;
                break;

            default:
                Debug.Fail("Unexpected Outcome.");
                break;
            }

            return(outcome);
        }
        /// <summary>
        /// Creates test result
        /// </summary>
        /// <param name="executionId"></param>
        /// <param name="parentExecutionId"></param>
        /// <param name="testType"></param>
        /// <param name="testElement"></param>
        /// <param name="parentTestElement"></param>
        /// <param name="parentTestResult"></param>
        /// <param name="rocksteadyTestResult"></param>
        /// <param name="ticketNumber"></param>
        /// <returns>Trx test result</returns>
        private ITestResult CreateTestResult(Guid executionId, Guid parentExecutionId, TestType testType,
                                             ITestElement testElement, ITestElement parentTestElement, ITestResult parentTestResult, Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult rocksteadyTestResult, string ticketNumber)
        {
            // Create test result
            TrxLoggerObjectModel.TestOutcome testOutcome = Converter.ToOutcome(rocksteadyTestResult.Outcome);
            var testResult = Converter.ToTestResult(testElement.Id.Id, executionId, parentExecutionId, testElement.Name,
                                                    this.testResultsDirPath, ticketNumber, testType, testElement.CategoryId, testOutcome, this.testRun, rocksteadyTestResult);

            // Normal result scenario
            if (parentTestResult == null)
            {
                this.results.TryAdd(executionId, testResult);
                return(testResult);
            }

            // Ordered test inner result scenario
            if (parentTestElement != null && parentTestElement.TestType.Equals(TrxLoggerConstants.OrderedTestType))
            {
                (parentTestResult as TestResultAggregation).InnerResults.Add(testResult);
                this.innerResults.TryAdd(executionId, testResult);
                return(testResult);
            }

            // Data driven inner result scenario
            if (parentTestElement != null && parentTestElement.TestType.Equals(TrxLoggerConstants.UnitTestType))
            {
                (parentTestResult as TestResultAggregation).InnerResults.Add(testResult);
                testResult.DataRowInfo      = (parentTestResult as TestResultAggregation).InnerResults.Count;
                testResult.ResultType       = TrxLoggerConstants.InnerDataDrivenResultType;
                parentTestResult.ResultType = TrxLoggerConstants.ParentDataDrivenResultType;
                return(testResult);
            }

            return(testResult);
        }
        /// <summary>
        /// Called when a test message is received.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// Event args
        /// </param>
        private void TestMessageHandler(object sender, TestRunMessageEventArgs e)
        {
            ValidateArg.NotNull <object>(sender, "sender");
            ValidateArg.NotNull <TestRunMessageEventArgs>(e, "e");

            TrxLoggerObjectModel.RunInfo runMessage;
            switch (e.Level)
            {
            case TestMessageLevel.Informational:
                this.AddRunLevelInformationalMessage(e.Message);
                break;

            case TestMessageLevel.Warning:
                runMessage = new TrxLoggerObjectModel.RunInfo(e.Message, null, Environment.MachineName, TrxLoggerObjectModel.TestOutcome.Warning);
                this.runLevelErrorsAndWarnings.Add(runMessage);
                break;

            case TestMessageLevel.Error:
                this.testRunOutcome = TrxLoggerObjectModel.TestOutcome.Failed;
                runMessage          = new TrxLoggerObjectModel.RunInfo(e.Message, null, Environment.MachineName, TrxLoggerObjectModel.TestOutcome.Error);
                this.runLevelErrorsAndWarnings.Add(runMessage);
                break;

            default:
                Debug.Fail("TrxLogger.TestMessageHandler: The test message level is unrecognized: {0}", e.Level.ToString());
                break;
            }
        }
Esempio n. 4
0
        public void SaveCounters(XmlElement xml, string location, int[] counters)
        {
            xml = (XmlElement)LocationToXmlNode(xml, location);

            for (int i = 0; i < counters.Length; i++)
            {
                TrxObjectModel.TestOutcome outcome = (TrxObjectModel.TestOutcome)i;
                string attributeName = outcome.ToString();
                attributeName = attributeName.Substring(0, 1).ToLowerInvariant() + attributeName.Substring(1);

                xml.SetAttribute(attributeName, counters[i].ToString(CultureInfo.InvariantCulture));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Converts the rockSteady result to unit test result
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="executionId"></param>
        /// <param name="parentExecutionId"></param>
        /// <param name="testName"></param>
        /// <param name="trxFileDirectory"></param>
        /// <param name="ticketNumber"></param>
        /// <param name="testType"></param>
        /// <param name="testCategoryId"></param>
        /// <param name="testOutcome"></param>
        /// <param name="testRun"></param>
        /// <param name="rockSteadyTestResult"></param>
        /// <returns>Trx test result object</returns>
        public static ITestResult ToTestResult(
            Guid testId,
            Guid executionId,
            Guid parentExecutionId,
            string testName,
            string trxFileDirectory,
            string ticketNumber,
            TestType testType,
            TestListCategoryId testCategoryId,
            TrxObjectModel.TestOutcome testOutcome,
            TestRun testRun,
            ObjectModel.TestResult rockSteadyTestResult)
        {
            var resultName = !string.IsNullOrEmpty(rockSteadyTestResult.DisplayName) ? rockSteadyTestResult.DisplayName : testName;
            var testResult = CreateTestResult(testRun.Id, testId, executionId, parentExecutionId, resultName, Environment.MachineName, ticketNumber, testOutcome, testType, testCategoryId);

            if (rockSteadyTestResult.ErrorMessage != null)
            {
                testResult.ErrorMessage = rockSteadyTestResult.ErrorMessage;
            }

            if (rockSteadyTestResult.ErrorStackTrace != null)
            {
                testResult.ErrorStackTrace = rockSteadyTestResult.ErrorStackTrace;
            }

            if (rockSteadyTestResult.EndTime != null)
            {
                testResult.EndTime = rockSteadyTestResult.EndTime.UtcDateTime;
            }

            if (rockSteadyTestResult.StartTime != null)
            {
                testResult.StartTime = rockSteadyTestResult.StartTime.UtcDateTime;
            }

            if (rockSteadyTestResult.Duration != null)
            {
                testResult.Duration = rockSteadyTestResult.Duration;
            }

            // Clear exsting messages and store rocksteady result messages.
            testResult.TextMessages = null;
            UpdateResultMessages(testResult, rockSteadyTestResult);

            // Save result attachments to target location.
            UpdateTestResultAttachments(rockSteadyTestResult, testResult, testRun, trxFileDirectory, true);

            return(testResult);
        }
Esempio n. 6
0
 /// <summary>
 /// Create test result.
 /// Currently trx supports only UnitTest and OrderedTest. All tests except OrderedTest all converted to unit test result.
 /// </summary>
 /// <param name="runId"></param>
 /// <param name="testId"></param>
 /// <param name="executionId"></param>
 /// <param name="parentExecutionId"></param>
 /// <param name="resultName"></param>
 /// <param name="computerName"></param>
 /// <param name="ticketNumber"></param>
 /// <param name="outcome"></param>
 /// <param name="testType"></param>
 /// <param name="testCategoryId"></param>
 /// <returns>Trx test result</returns>
 private static TrxObjectModel.TestResult CreateTestResult(
     Guid runId,
     Guid testId,
     Guid executionId,
     Guid parentExecutionId,
     string resultName,
     string computerName,
     string ticketNumber,
     TrxObjectModel.TestOutcome outcome,
     TestType testType,
     TestListCategoryId testCategoryId)
 {
     return(testType.Equals(Constants.OrderedTestType) ?
            new TestResultAggregation(runId, testId, executionId, parentExecutionId, resultName, Environment.MachineName, ticketNumber, outcome, testType, testCategoryId) :
            new UnitTestResult(runId, testId, executionId, parentExecutionId, resultName, Environment.MachineName, ticketNumber, outcome, testType, testCategoryId));
 }
        /// <summary>
        /// Called when a test run is completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// Test run complete events arguments.
        /// </param>
        public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
        {
            // Create test run
            // If abort occurs there is no call to TestResultHandler which results in testRun not created.
            // This happens when some test aborts in the first batch of execution.
            if (this.testRun == null)
            {
                CreateTestRun();
            }

            XmlPersistence         helper      = new XmlPersistence();
            XmlTestStoreParameters parameters  = XmlTestStoreParameters.GetParameters();
            XmlElement             rootElement = helper.CreateRootElement("TestRun");

            // Save runId/username/creation time etc.
            this.testRun.Finished = DateTime.UtcNow;
            helper.SaveSingleFields(rootElement, this.testRun, parameters);

            // Save test settings
            helper.SaveObject(this.testRun.RunConfiguration, rootElement, "TestSettings", parameters);

            // Save test results
            helper.SaveIEnumerable(this.results.Values, rootElement, "Results", ".", null, parameters);

            // Save test definitions
            helper.SaveIEnumerable(this.testElements.Values, rootElement, "TestDefinitions", ".", null, parameters);

            // Save test entries
            helper.SaveIEnumerable(this.entries.Values, rootElement, "TestEntries", ".", "TestEntry", parameters);

            // Save default categories
            List <TestListCategory> categories = new List <TestListCategory>();

            categories.Add(TestListCategory.UncategorizedResults);
            categories.Add(TestListCategory.AllResults);
            helper.SaveList <TestListCategory>(categories, rootElement, "TestLists", ".", "TestList", parameters);

            // Save summary
            if (this.testRunOutcome == TrxLoggerObjectModel.TestOutcome.Passed)
            {
                this.testRunOutcome = TrxLoggerObjectModel.TestOutcome.Completed;
            }

            List <string>             errorMessages    = new List <string>();
            List <CollectorDataEntry> collectorEntries = Converter.ToCollectionEntries(e.AttachmentSets, this.testRun, this.testResultsDirPath);
            IList <String>            resultFiles      = Converter.ToResultFiles(e.AttachmentSets, this.testRun, this.testResultsDirPath, errorMessages);

            if (errorMessages.Count > 0)
            {
                // Got some errors while attaching files, report them and set the outcome of testrun to be Error...
                this.testRunOutcome = TrxLoggerObjectModel.TestOutcome.Error;
                foreach (string msg in errorMessages)
                {
                    RunInfo runMessage = new RunInfo(msg, null, Environment.MachineName, TrxLoggerObjectModel.TestOutcome.Error);
                    this.runLevelErrorsAndWarnings.Add(runMessage);
                }
            }

            TestRunSummary runSummary = new TestRunSummary(
                this.totalTests,
                this.passTests + this.failTests,
                this.passTests,
                this.failTests,
                this.testRunOutcome,
                this.runLevelErrorsAndWarnings,
                this.runLevelStdOut.ToString(),
                resultFiles,
                collectorEntries);

            helper.SaveObject(runSummary, rootElement, "ResultSummary", parameters);

            //Save results to Trx file
            this.DeriveTrxFilePath();
            this.PopulateTrxFile(this.trxFilePath, rootElement);
        }
        /// <summary>
        /// Called when a test result is received.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The eventArgs.
        /// </param>
        public void TestResultHandler(object sender, TestResultEventArgs e)
        {
            // Create test run
            if (this.testRun == null)
            {
                CreateTestRun();
            }

            // Convert skipped test to a log entry as that is the behaviour of mstest.
            if (e.Result.Outcome == Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Skipped)
            {
                this.HandleSkippedTest(e.Result);
            }

            var testType    = Converter.GetTestType(e.Result);
            var executionId = Converter.GetExecutionId(e.Result);

            // Setting parent properties like parent result, parent test element, parent execution id.
            var parentExecutionId = Converter.GetParentExecutionId(e.Result);
            var parentTestResult  = GetTestResult(parentExecutionId);
            var parentTestElement = (parentTestResult != null) ? GetTestElement(parentTestResult.Id.TestId) : null;

            // Retrieve Ticket number if it exists
            var ticketNumber = "";

            if (Converter.GetTicketNumber(e.Result.TestCase) != null)
            {
                ticketNumber = Converter.GetTicketNumber(e.Result.TestCase)[0];
            }

            Console.WriteLine("Number: " + ticketNumber);
            // Switch to flat test results in case any parent related information is missing.
            if (parentTestResult == null || parentTestElement == null || parentExecutionId == Guid.Empty)
            {
                parentTestResult  = null;
                parentTestElement = null;
                parentExecutionId = Guid.Empty;
            }

            // Create trx test element from rocksteady test case
            var testElement = GetOrCreateTestElement(executionId, parentExecutionId, testType, parentTestElement, e.Result);

            // Update test links. Test Links are updated in case of Ordered test.
            UpdateTestLinks(testElement, parentTestElement);

            // Convert the rocksteady result to trx test result
            var testResult = CreateTestResult(executionId, parentExecutionId, testType, testElement, parentTestElement, parentTestResult, e.Result, ticketNumber);

            // Update test entries
            UpdateTestEntries(executionId, parentExecutionId, testElement, parentTestElement);

            // 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++;
            }
        }