Exemplo n.º 1
0
        private static void RunTestMethod(IMessageBus messageBus,
                                          object[] constructorArguments,
                                          IEnumerable <XunitTestCase> testCases,
                                          RunSummary classSummary,
                                          ExceptionAggregator aggregator,
                                          CancellationTokenSource cancellationTokenSource)
        {
            foreach (var testCase in testCases)
            {
                using (var delegatingBus = new DelegatingMessageBus <ITestCaseFinished>(messageBus))
                {
                    testCase.Run(delegatingBus, constructorArguments, aggregator, cancellationTokenSource);
                    delegatingBus.Finished.WaitOne();

                    classSummary.Total   += delegatingBus.FinalMessage.TestsRun;
                    classSummary.Failed  += delegatingBus.FinalMessage.TestsFailed;
                    classSummary.Skipped += delegatingBus.FinalMessage.TestsSkipped;
                    classSummary.Time    += delegatingBus.FinalMessage.ExecutionTime;
                }

                if (cancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public virtual async Task <RunSummary> RunAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            var summary = new RunSummary();

            if (!messageBus.QueueMessage(new TestCaseStarting(this)))
            {
                cancellationTokenSource.Cancel();
            }
            else
            {
                using (var delegatingBus = new DelegatingMessageBus(messageBus,
                                                                    msg =>
                {
                    if (msg is ITestResultMessage)
                    {
                        summary.Total++;
                        summary.Time += ((ITestResultMessage)msg).ExecutionTime;
                    }
                    if (msg is ITestFailed)
                    {
                        summary.Failed++;
                    }
                    if (msg is ITestSkipped)
                    {
                        summary.Skipped++;
                    }
                }))
                {
                    await RunTestsAsync(delegatingBus, constructorArguments, aggregator, cancellationTokenSource);
                }
            }

            if (!messageBus.QueueMessage(new TestCaseFinished(this, summary.Time, summary.Total, summary.Failed, summary.Skipped)))
            {
                cancellationTokenSource.Cancel();
            }

            return(summary);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes the test case, returning 0 or more result messages through the message sink.
        /// </summary>
        /// <param name="messageBus">The message bus to report results to.</param>
        /// <param name="constructorArguments">The arguments to pass to the constructor.</param>
        /// <param name="aggregator">The error aggregator to use for catching exception.</param>
        /// <param name="cancellationTokenSource">The cancellation token source that indicates whether cancellation has been requested.</param>
        public virtual void Run(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            int     totalFailed   = 0;
            int     totalRun      = 0;
            int     totalSkipped  = 0;
            decimal executionTime = 0M;

            if (!messageBus.QueueMessage(new TestCaseStarting(this)))
            {
                cancellationTokenSource.Cancel();
            }
            else
            {
                using (var delegatingBus = new DelegatingMessageBus(messageBus, msg =>
                {
                    if (msg is ITestResultMessage)
                    {
                        totalRun++;
                        executionTime += ((ITestResultMessage)msg).ExecutionTime;
                    }
                    if (msg is ITestFailed)
                    {
                        totalFailed++;
                    }
                    if (msg is ITestSkipped)
                    {
                        totalSkipped++;
                    }
                }))
                    RunTests(delegatingBus, constructorArguments, aggregator, cancellationTokenSource);
            }

            if (!messageBus.QueueMessage(new TestCaseFinished(this, executionTime, totalRun, totalFailed, totalSkipped)))
            {
                cancellationTokenSource.Cancel();
            }
        }
Exemplo n.º 4
0
        private static async Task RunTestMethodAsync(IMessageBus messageBus,
                                                     object[] constructorArguments,
                                                     IEnumerable<XunitTestCase> testCases,
                                                     RunSummary classSummary,
                                                     ExceptionAggregator aggregator,
                                                     CancellationTokenSource cancellationTokenSource)
        {
            foreach (var testCase in testCases)
            {
                using (var delegatingBus = new DelegatingMessageBus<ITestCaseFinished>(messageBus))
                {
                    await testCase.RunAsync(delegatingBus, constructorArguments, aggregator, cancellationTokenSource);
                    delegatingBus.Finished.WaitOne();

                    classSummary.Total += delegatingBus.FinalMessage.TestsRun;
                    classSummary.Failed += delegatingBus.FinalMessage.TestsFailed;
                    classSummary.Skipped += delegatingBus.FinalMessage.TestsSkipped;
                    classSummary.Time += delegatingBus.FinalMessage.ExecutionTime;
                }

                if (cancellationTokenSource.IsCancellationRequested)
                    break;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the test case, returning 0 or more result messages through the message sink.
        /// </summary>
        /// <param name="messageBus">The message bus to report results to.</param>
        /// <param name="constructorArguments">The arguments to pass to the constructor.</param>
        /// <param name="aggregator">The error aggregator to use for catching exception.</param>
        /// <param name="cancellationTokenSource">The cancellation token source that indicates whether cancellation has been requested.</param>
        public virtual async Task RunAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            int totalFailed = 0;
            int totalRun = 0;
            int totalSkipped = 0;
            decimal executionTime = 0M;

            if (!messageBus.QueueMessage(new TestCaseStarting(this)))
                cancellationTokenSource.Cancel();
            else
            {
                using (var delegatingBus = new DelegatingMessageBus(messageBus,
                    msg =>
                    {
                        if (msg is ITestResultMessage)
                        {
                            totalRun++;
                            executionTime += ((ITestResultMessage)msg).ExecutionTime;
                        }
                        if (msg is ITestFailed)
                            totalFailed++;
                        if (msg is ITestSkipped)
                            totalSkipped++;
                    }))
                {
                    await RunTestsAsync(delegatingBus, constructorArguments, aggregator, cancellationTokenSource);
                }
            }

            if (!messageBus.QueueMessage(new TestCaseFinished(this, executionTime, totalRun, totalFailed, totalSkipped)))
                cancellationTokenSource.Cancel();
        }
Exemplo n.º 6
0
        private async Task<RunSummary> InvokeStepsAsync(
            ICollection<IStepDefinition> backGroundStepDefinitions, ICollection<IStepDefinition> scenarioStepDefinitions)
        {
            var filters = this.scenarioClass.Assembly.GetCustomAttributes(typeof(Attribute))
                .Concat(this.scenarioClass.GetCustomAttributes(typeof(Attribute)))
                .Concat(this.scenarioMethod.GetCustomAttributes(typeof(Attribute)))
                .OfType<IFilter<IStepDefinition>>();

            var stepDefinitions = filters
                .Aggregate(
                    backGroundStepDefinitions.Concat(scenarioStepDefinitions),
                    (current, filter) => filter.Filter(current))
                .ToArray();

            var summary = new RunSummary();
            string skipReason = null;
            var teardowns = new List<Action>();
            var stepNumber = 0;
            foreach (var stepDefinition in stepDefinitions)
            {
                stepDefinition.SkipReason = stepDefinition.SkipReason ?? skipReason;

                var stepDisplayName = GetStepDisplayName(
                    this.scenario.DisplayName,
                    ++stepNumber,
                    stepNumber <= backGroundStepDefinitions.Count,
                    stepDefinition.Text,
                    this.scenarioMethodArguments);

                var step = new Step(this.scenario, stepDisplayName);

                var interceptingBus = new DelegatingMessageBus(
                    this.messageBus,
                    message =>
                    {
                        if (message is ITestFailed && stepDefinition.FailureBehavior == RemainingSteps.Skip)
                        {
                            skipReason = string.Format(
                                CultureInfo.InvariantCulture,
                                "Failed to execute preceding step: {0}",
                                step.DisplayName);
                        }
                    });

                var stepRunner = new StepRunner(
                    step,
                    stepDefinition.Body,
                    interceptingBus,
                    this.scenarioClass,
                    this.constructorArguments,
                    this.scenarioMethod,
                    this.scenarioMethodArguments,
                    stepDefinition.SkipReason,
                    new ExceptionAggregator(this.aggregator),
                    this.cancellationTokenSource);

                summary.Aggregate(await stepRunner.RunAsync());
                teardowns.AddRange(stepRunner.Disposables.Select(disposable => (Action)disposable.Dispose)
                    .Concat(stepDefinition.Teardowns.Where(teardown => teardown != null)).ToArray());
            }

            if (teardowns.Any())
            {
                teardowns.Reverse();
                var teardownTimer = new ExecutionTimer();
                var teardownAggregator = new ExceptionAggregator();
                foreach (var teardown in teardowns)
                {
                    teardownTimer.Aggregate(() => teardownAggregator.Run(() => teardown()));
                }

                summary.Time += teardownTimer.Total;

                if (teardownAggregator.HasExceptions)
                {
                    summary.Failed++;
                    summary.Total++;

                    var stepDisplayName = GetStepDisplayName(
                        this.scenario.DisplayName,
                        ++stepNumber,
                        false,
                        "(Teardown)",
                        this.scenarioMethodArguments);

                    this.messageBus.Queue(
                        new Step(this.scenario, stepDisplayName),
                        test => new TestFailed(test, teardownTimer.Total, null, teardownAggregator.ToException()),
                        this.cancellationTokenSource);
                }
            }

            return summary;
        }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public virtual async Task<RunSummary> RunAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            var summary = new RunSummary();

            if (!messageBus.QueueMessage(new TestCaseStarting(this)))
                cancellationTokenSource.Cancel();
            else
            {
                using (var delegatingBus = new DelegatingMessageBus(messageBus,
                    msg =>
                    {
                        if (msg is ITestResultMessage)
                        {
                            summary.Total++;
                            summary.Time += ((ITestResultMessage)msg).ExecutionTime;
                        }
                        if (msg is ITestFailed)
                            summary.Failed++;
                        if (msg is ITestSkipped)
                            summary.Skipped++;
                    }))
                {
                    await RunTestsAsync(delegatingBus, constructorArguments, aggregator, cancellationTokenSource);
                }
            }

            if (!messageBus.QueueMessage(new TestCaseFinished(this, summary.Time, summary.Total, summary.Failed, summary.Skipped)))
                cancellationTokenSource.Cancel();

            return summary;
        }