Exemplo n.º 1
0
        private async Task <RunSummary> RunTestCaseWithMethodFixturesAsync(IXunitTestCase testCase, TaskObserver taskObserver, TestMainThread testMainThread)
        {
            var runSummary     = new RunSummary();
            var methodFixtures = CreateMethodFixtures(testCase, runSummary);

            if (Aggregator.HasExceptions)
            {
                return(runSummary);
            }

            var testCaseConstructorArguments = await InitializeMethodFixturesAsync(testCase, runSummary, methodFixtures);

            if (!Aggregator.HasExceptions)
            {
                var testCaseRunTask    = testCase.RunAsync(_diagnosticMessageSink, MessageBus, testCaseConstructorArguments, new ExceptionAggregator(Aggregator), CancellationTokenSource);
                var testCaseRunSummary = await GetTestRunSummary(testCaseRunTask, taskObserver.Task);

                runSummary.Aggregate(testCaseRunSummary);
            }

            await DisposeMethodFixturesAsync(testCase, runSummary, methodFixtures);
            await WaitForObservedTasksAsync(testCase, runSummary, taskObserver, testMainThread);

            return(runSummary);
        }
Exemplo n.º 2
0
    public async Task <RunSummary> RunAsync(IXunitTestCase testCase, IServiceProvider provider, IMessageSink diagnosticMessageSink, IMessageBus messageBus, object?[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
    {
        var scope = provider.CreateScope();

        await using var _ = scope.AsAsyncDisposable().ConfigureAwait(false);

        var raw = new Dictionary <int, object>();

        foreach (var kv in FromServicesAttribute.CreateFromServices(testCase.Method.ToRuntimeMethod()))
        {
            raw[kv.Key] = testCase.TestMethodArguments[kv.Key];

            testCase.TestMethodArguments[kv.Key] = kv.Value == typeof(ITestOutputHelper)
                ? throw new NotSupportedException("Can't inject ITestOutputHelper via method arguments when use StaFact")
                : provider.GetService(kv.Value);
        }

        constructorArguments = DependencyInjectionTestMethodRunner.CreateTestClassConstructorArguments(scope.ServiceProvider, constructorArguments, aggregator);

        var summary = await testCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource).ConfigureAwait(false);

        foreach (var kv in raw)
        {
            testCase.TestMethodArguments[kv.Key] = kv.Value;
        }

        return(summary);
    }
 protected override Task <RunSummary> RunTestCaseAsync(IXunitTestCase testCase)
 {
     if (testCase is TestCase customTestCase)
     {
         customTestCase.SetClassFixtureMappings(_classFixtureMappings);
         customTestCase.SetAssemblyTestNotificationType(_testNotificationType);
     }
     return(testCase.RunAsync(_diagnosticMessageSink, MessageBus, _constructorArguments, new ExceptionAggregator(Aggregator), CancellationTokenSource));
 }
        protected override async Task <RunSummary> RunTestCaseAsync(IXunitTestCase testCase)
        {
            if (testCase is AutofacTestCase autofacTestCase)
            {
                autofacTestCase.TestClassLifetimeScope = _testClassLifetimeScope;
            }

            return(await testCase.RunAsync(_diagnosticMessageSink, MessageBus, new object[] { }, Aggregator, CancellationTokenSource));
        }
Exemplo n.º 5
0
        private Task <RunSummary> RunTestCaseAsync(IXunitTestCase xunitTestCase, object[] constructorArguments)
        {
            if (xunitTestCase is TestCase testCase)
            {
                testCase.MainThreadFixture = _testMainThreadFixture;
            }

            return(xunitTestCase.RunAsync(_diagnosticMessageSink, MessageBus, constructorArguments, new ExceptionAggregator(Aggregator), CancellationTokenSource));
        }
Exemplo n.º 6
0
        protected override async Task <RunSummary> RunTestCaseAsync(IXunitTestCase testCase)
        {
            if (!_hasMethodFixtures)
            {
                return(await base.RunTestCaseAsync(testCase));
            }

            var methodFixtureTypes = _constructorArguments
                                     .OfType <IMethodFixture>()
                                     .Select(f => f.GetType())
                                     .ToList();

            var methodFixtureFactories = _assemblyFixtureMappings.Values
                                         .OfType <IMethodFixtureFactory <object> >()
                                         .ToList();

            IDictionary <Type, object> methodFixtures;

            try {
                methodFixtures = MethodFixtureTypes.CreateMethodFixtures(methodFixtureTypes, methodFixtureFactories);
            } catch (Exception exception) {
                var runSummary = new RunSummary {
                    Total = 1, Failed = 1, Time = 0
                };
                MessageBus.QueueMessage(new TestFailed(new XunitTest(testCase, testCase.DisplayName), runSummary.Time, string.Empty, exception));
                return(runSummary);
            }

            var testCaseConstructorArguments = GetConstructorArguments(methodFixtures);

            var testInput = new TestInput(testCase,
                                          Class.Type,
                                          TestMethod.Method.ToRuntimeMethod(),
                                          testCase.DisplayName,
                                          testCaseConstructorArguments,
                                          testCase.TestMethodArguments);

            var tasks = await InitializeMethodFixturesAsync(testInput, methodFixtures, MessageBus);

            var runTestTask = testCase.RunAsync(_diagnosticMessageSink, MessageBus, testCaseConstructorArguments, new ExceptionAggregator(Aggregator), CancellationTokenSource);

            tasks.Add(runTestTask);

            var runSummaryTask = await Task.WhenAny(tasks);

            await runSummaryTask;

            if (runSummaryTask != runTestTask)
            {
                CancellationTokenSource.Cancel();
            }

            await DisposeMethodFixturesAsync(runSummaryTask.Result, methodFixtures, MessageBus);

            return(runSummaryTask.Result);
        }
Exemplo n.º 7
0
        public Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink,
                                          IMessageBus messageBus,
                                          object[] constructorArguments,
                                          ExceptionAggregator aggregator,
                                          CancellationTokenSource cancellationTokenSource)
        {
            var taskCompletionSource = new TaskCompletionSource <RunSummary>();
            var thread = new Thread(() =>
            {
                try
                {
                    // Set up the SynchronizationContext so that any awaits
                    // resume on the STA thread as they would in a GUI app.
                    SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext());

                    // Start off the test method.
                    var testCaseTask = _testCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource);

                    // Arrange to pump messages to execute any async work associated with the test.
                    var frame = new DispatcherFrame();
                    _         = Task.Run(async delegate
                    {
                        try
                        {
                            await testCaseTask;
                        }
                        finally
                        {
                            // The test case's execution is done. Terminate the message pump.
                            frame.Continue = false;
                        }
                    });
                    Dispatcher.PushFrame(frame);

                    // Report the result back to the Task we returned earlier.
                    CopyTaskResultFrom(taskCompletionSource, testCaseTask);
                }
                catch (Exception e)
                {
                    taskCompletionSource.SetException(e);
                }
                finally
                {
                    Dispatcher.CurrentDispatcher.InvokeShutdown();
                }
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            return(taskCompletionSource.Task);
        }
 protected override async Task <RunSummary> RunTestCaseAsync(IXunitTestCase testCase)
 {
     using (var scope = _lifetimeScope.BeginLifetimeScope(cb =>
     {
         cb
         .RegisterInstance(testCase)
         .As <ITestCase>()
         .ExternallyOwned();
     }))
     {
         object[] constructorArguments = null;
         Aggregator.Run(() => constructorArguments = ResolveConstructorArguments(scope));
         return(await testCase.RunAsync(_diagnosticMessageSink, MessageBus, constructorArguments, new ExceptionAggregator(Aggregator), CancellationTokenSource));
     }
 }
Exemplo n.º 9
0
    public Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments,
                                      ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
    {
        var tcs    = new TaskCompletionSource <RunSummary>();
        var thread = new Thread(() =>
        {
            try
            {
                var testCaseTask = testCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator,
                                                     cancellationTokenSource);
                tcs.SetResult(testCaseTask.Result);
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        return(tcs.Task);
    }
Exemplo n.º 10
0
        public Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments,
                                          ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            var tcs = new TaskCompletionSource <RunSummary>();

            SampleProcess.Program.Scheduler.ScheduleAsync(async(scheduler, ct) =>
            {
                try
                {
                    var runSummary = await _TestCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource);
                    tcs.SetResult(runSummary);
                }
                catch (OperationCanceledException)
                {
                    tcs.SetCanceled();
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
            });
            return(tcs.Task);
        }
 /// <inheritdoc/>
 protected override Task<RunSummary> RunTestCaseAsync(IXunitTestCase testCase)
     => testCase.RunAsync(DiagnosticMessageSink, MessageBus, ConstructorArguments, new ExceptionAggregator(Aggregator), CancellationTokenSource);
Exemplo n.º 12
0
 /// <inheritdoc/>
 protected override Task <RunSummary> RunTestCaseAsync(IXunitTestCase testCase)
 => testCase.RunAsync(this.DiagnosticMessageSink, this.MessageBus, this.ConstructorArguments, new ExceptionAggregator(this.Aggregator), this.CancellationTokenSource);