Example #1
0
        public async Task <RunSummary> RunAsync()
        {
            OnTestCollectionStarting();

            var collectionSummary = new RunSummary();

            if (MessageBus.QueueMessage(new TestCollectionStarting(TestCollection)))
            {
                foreach (var testCasesByClass in TestCases.GroupBy(tc => tc.Class))
                {
                    var classSummary = await RunTestClassAsync((IReflectionTypeInfo)testCasesByClass.Key, testCasesByClass);

                    collectionSummary.Aggregate(classSummary);

                    if (CancellationTokenSource.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }

            if (!MessageBus.QueueMessage(new TestCollectionFinished(TestCollection, collectionSummary.Time, collectionSummary.Total, collectionSummary.Failed, collectionSummary.Skipped)))
            {
                CancellationTokenSource.Cancel();
            }

            OnTestCollectionFinished();

            return(collectionSummary);
        }
        protected async override Task <RunSummary> RunTestClassesAsync()
        {
            var groups = TestCases
                         .GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance);

            if (TestCaseOrderer is TestCaseOrderer orderer)
            {
                groups = orderer.OrderTestClasses(groups);
            }

            var summary = new RunSummary();

            foreach (IGrouping <ITestClass, IXunitTestCase> testCasesByClass in groups)
            {
                summary.Aggregate(
                    await RunTestClassAsync(
                        testCasesByClass.Key,
                        (IReflectionTypeInfo)testCasesByClass.Key.Class,
                        testCasesByClass));

                if (CancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
            }

            return(summary);
        }
        /// <summary>
        /// Runs the list of test classes. By default, groups the tests by class and runs them synchronously.
        /// </summary>
        /// <returns>Returns summary information about the tests that were run.</returns>
        protected virtual async Task <RunSummary> RunTestClassesAsync()
        {
            var summary = new RunSummary();

            foreach (var testCasesByClass in TestCases.GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance))
            {
                summary.Aggregate(await RunTestClassAsync(testCasesByClass.Key, (IReflectionTypeInfo)testCasesByClass.Key.Class, testCasesByClass));
                if (CancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
            }

            return(summary);
        }
        private async Task <RunSummary> RunTestClassesInParallelAsync()
        {
            var summary = new RunSummary();

            var parallelOptions = new ParallelOptions
            {
                CancellationToken      = CancellationTokenSource.Token,
                MaxDegreeOfParallelism = _maxParallelThreads
            };

            var testCasesGroupedByClass = TestCases.GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance);
            var task = testCasesGroupedByClass.ForEachAsync(parallelOptions,
                                                            async testCasesByClass =>
            {
                var partialSummary = await RunTestClassAsync(testCasesByClass.Key, (IReflectionTypeInfo)testCasesByClass.Key.Class, testCasesByClass);
                summary.Aggregate(partialSummary);
            });

            await task;

            return(summary);
        }
        protected async override Task <RunSummary> RunTestClassesAsync()
        {
            var groups = TestCases
                         .GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance);

            try
            {
                if (TestCaseOrderer is ITestClassOrderer orderer)
                {
                    groups = orderer.OrderTestClasses(groups);
                }
            }
            catch (Exception ex)
            {
                if ((ex is TargetInvocationException tiex))
                {
                    ex = ex.InnerException;
                }
                DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Test class orderer '{TestCaseOrderer.GetType().FullName}' threw '{ex.GetType().FullName}' during ordering: {ex.Message}{Environment.NewLine}{ex.StackTrace}"));
            }
            var summary = new RunSummary();

            foreach (IGrouping <ITestClass, IXunitTestCase> testCasesByClass in groups)
            {
                summary.Aggregate(
                    await RunTestClassAsync(
                        testCasesByClass.Key,
                        (IReflectionTypeInfo)testCasesByClass.Key.Class,
                        testCasesByClass));

                if (CancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
            }

            return(summary);
        }
Example #6
0
        /// <inheritdoc/>
        protected override async Task <RunSummary> RunTestCollectionsAsync(IMessageBus messageBus, CancellationTokenSource cancellationTokenSource)
        {
            if (disableParallelization)
            {
                return(await base.RunTestCollectionsAsync(messageBus, cancellationTokenSource).ConfigureAwait(false));
            }

            var tasks = TestCases.GroupBy(tc => tc.TestMethod.TestClass.TestCollection, TestCollectionComparer.Instance)
                        .Select(collectionGroup => Task.Factory.StartNew(() => RunTestCollectionAsync(messageBus, collectionGroup.Key, collectionGroup, cancellationTokenSource),
                                                                         cancellationTokenSource.Token,
                                                                         TaskCreationOptions.DenyChildAttach,
                                                                         scheduler))
                        .ToArray();

            var summaries = await Task.WhenAll(tasks.Select(t => t.Unwrap())).ConfigureAwait(false);

            return(new RunSummary()
            {
                Total = summaries.Sum(s => s.Total),
                Failed = summaries.Sum(s => s.Failed),
                Skipped = summaries.Sum(s => s.Skipped)
            });
        }