Ejemplo n.º 1
0
        /// <summary>
        /// Maps result types from TRX into instance of drop models with configuration
        /// </summary>
        /// <param name="source">Instance of test results from deserialised TRX input</param>
        /// <param name="destination">Instance to map and merge results into</param>
        /// <param name="inputConfiguration">User configured input for current source</param>
        public static void Map(TestRunType source, TestRunDrop destination, ReportInput inputConfiguration = null)
        {
            var times    = source.Times.FirstOrDefault();
            var started  = DateTimeOffset.Parse(times.Start);
            var finished = DateTimeOffset.Parse(times.Finish);

            if (destination.Started is null || destination.Started > started)
            {
                destination.Started = started;
            }
            if (destination.Finished is null || destination.Finished < finished)
            {
                destination.Finished = finished;
            }

            var definitions          = source.TestDefinitions.SelectMany(x => x.UnitTest).ToDictionary(k => k.Id, v => v);
            var unitTestResultsGroup = source.Results
                                       .SelectMany(r => r.UnitTestResult)
                                       .Select(r => (result: r, definition: definitions[r.TestId]))
                                       .GroupBy(t => inputConfiguration?.GroupTitle ?? StringFilters.PathSplit(t.definition.TestMethod.CodeBase).Last()) // Group by codebase if no title is provided
                                       .ToList();

            foreach (var resultGroup in unitTestResultsGroup)
            {
                TestResultSetDrop drop;

                if (destination.ResultSets.TryGetValue(resultGroup.Key, out var existingDrop))
                {
                    drop = existingDrop;
                }
                else
                {
                    drop = new TestResultSetDrop
                    {
                        Source  = resultGroup.Key,
                        Results = new List <TestResultDrop>(),
                    };
                    destination.ResultSets.Add(drop);
                }

                foreach (var(result, definition) in resultGroup)
                {
                    foreach (var unitTestResults in ExtractTestResults(result))
                    {
                        var testCase = new TestCaseDrop
                        {
                            Source             = definition.TestMethod.CodeBase,
                            DisplayName        = string.IsNullOrEmpty(inputConfiguration?.TestSuffix) ? unitTestResults.TestName : $"{unitTestResults.TestName}{inputConfiguration.TestSuffix}",
                            FullyQualifiedName = $"{definition.TestMethod.ClassName}.{definition.TestMethod.Name}",
                            Id          = Guid.Parse(definition.Id),
                            ExecutorUri = definition.TestMethod.AdapterTypeName,
                        };
                        var startTime  = DateTimeOffset.Parse(unitTestResults.StartTime);
                        var endTime    = DateTimeOffset.Parse(unitTestResults.EndTime);
                        var duration   = (endTime - startTime);
                        var outcome    = MapOutcome(unitTestResults.Outcome, drop, destination.TestRunStatistics);
                        var resultDrop = new TestResultDrop
                        {
                            StartTime      = startTime,
                            EndTime        = endTime,
                            Duration       = duration,
                            Outcome        = outcome,
                            TestCase       = testCase,
                            ComputerName   = unitTestResults.ComputerName,
                            AttachmentSets = new List <AttachmentSetDrop>(unitTestResults.CollectorDataEntries.Select(rf => new AttachmentSetDrop
                            {
                                Attachments = new List <AttachmentDrop>(rf.Collector.Select(c => new AttachmentDrop
                                {
                                    Description = c.CollectorDisplayName,
                                    Uri         = c.Uri
                                }))
                            })),
                            DisplayName = definition.TestMethod.Name
                        };
                        MapOutputToResult(unitTestResults.Output, resultDrop);
                        destination.TestRunStatistics.ExecutedTestsCount++;
                        destination.ElapsedTimeInRunningTests += duration;
                        drop.Duration += duration;
                        drop.ExecutedTestsCount++;
                        drop.Results.Add(resultDrop);
                    }
                }
            }
        }