public ScenarioOutlineRunner(
     IMessageSink diagnosticMessageSink,
     IXunitTestCase scenarioOutline,
     string displayName,
     string skipReason,
     object[] constructorArguments,
     IMessageBus messageBus,
     ExceptionAggregator aggregator,
     CancellationTokenSource cancellationTokenSource)
     : base(
         scenarioOutline,
         displayName,
         skipReason,
         constructorArguments,
         noArguments,
         messageBus,
         aggregator,
         cancellationTokenSource)
 {
     this.diagnosticMessageSink = diagnosticMessageSink;
     this.scenarioRunnerFactory = new ScenarioRunnerFactory(
         this.TestCase,
         this.DisplayName,
         this.MessageBus,
         this.TestClass,
         this.ConstructorArguments,
         this.SkipReason,
         this.BeforeAfterAttributes,
         this.Aggregator,
         this.CancellationTokenSource);
 }
예제 #2
0
        public ScenarioRunner(
            IScenario scenario,
            IMessageBus messageBus,
            Type scenarioClass,
            object[] constructorArguments,
            MethodInfo scenarioMethod,
            object[] scenarioMethodArguments,
            string skipReason,
            IReadOnlyList<BeforeAfterTestAttribute> beforeAfterScenarioAttributes,
            ExceptionAggregator aggregator,
            CancellationTokenSource cancellationTokenSource)
        {
            Guard.AgainstNullArgument("scenario", scenario);
            Guard.AgainstNullArgument("messageBus", messageBus);
            Guard.AgainstNullArgument("aggregator", aggregator);

            this.scenario = scenario;
            this.messageBus = messageBus;
            this.scenarioClass = scenarioClass;
            this.constructorArguments = constructorArguments;
            this.scenarioMethod = scenarioMethod;
            this.scenarioMethodArguments = scenarioMethodArguments;
            this.skipReason = skipReason;
            this.beforeAfterScenarioAttributes = beforeAfterScenarioAttributes;
            this.parentAggregator = aggregator;
            this.cancellationTokenSource = cancellationTokenSource;
        }
 public override Task<RunSummary> RunAsync(SpecificationBase specification,
                                  IMessageBus messageBus,
                                  ExceptionAggregator aggregator,
                                  CancellationTokenSource cancellationTokenSource)
 {
     return Task.FromResult(new RunSummary { Total = 1, Skipped = 1  });
 }
예제 #4
0
        public Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            
            var tcs = new TaskCompletionSource<RunSummary>();



#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed


            // Run on the UI thread
            Device.BeginInvokeOnMainThread(
                () =>
                {
                    try
                    {
                        var result = testCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource);
                        result.ContinueWith(t =>
                                            {
                                                if (t.IsFaulted)
                                                    tcs.SetException(t.Exception);

                                                tcs.SetResult(t.Result);
                                            });
                    }
                    catch (Exception e)
                    {
                        tcs.SetException(e);
                    }

                });
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            return tcs.Task;
        }
예제 #5
0
 /// <inheritdoc/>
 public override Task<RunSummary> RunAsync(IMessageBus messageBus,
                                           object[] constructorArguments,
                                           ExceptionAggregator aggregator,
                                           CancellationTokenSource cancellationTokenSource)
 {
     return new LambdaTestCaseRunner(this, messageBus, aggregator, cancellationTokenSource).RunAsync();
 }
예제 #6
0
        void RunTestImpl(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, TaskCompletionSource<RunSummary> tcs)
        {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            var disp = CoreApplication.MainView.Dispatcher;

            // Run on the current window's dispatcher
            disp.RunAsync(CoreDispatcherPriority.Normal,
                          () =>
                          {
                              try
                              {
                                  var result = testCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource);
                                  result.ContinueWith(t =>
                                  {
                                      if (t.IsFaulted)
                                          tcs.SetException(t.Exception);

                                      tcs.SetResult(t.Result);
                                  });
                              }
                              catch (Exception e)
                              {
                                  tcs.TrySetException(e);
                              }
                          }
                );

#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
예제 #7
0
        /// <inheritdoc/>
        protected override bool RunTests(IMessageSink messageSink, object[] constructorArguments, ExceptionAggregator aggregator)
        {
            bool cancelled = false;

            if (!messageSink.OnMessage(new TestStarting { TestCase = this, TestDisplayName = DisplayName }))
                cancelled = true;
            else
            {
                try
                {
                    lambda();

                    if (!messageSink.OnMessage(new TestPassed { TestCase = this, TestDisplayName = DisplayName }))
                        cancelled = true;
                }
                catch (Exception ex)
                {
                    if (!messageSink.OnMessage(new TestFailed(ex) { TestCase = this, TestDisplayName = DisplayName }))
                        cancelled = true;
                }
            }

            if (!messageSink.OnMessage(new TestFinished { TestCase = this, TestDisplayName = DisplayName }))
                cancelled = true;

            return cancelled;
        }
예제 #8
0
        /// <inheritdoc/>
        protected override Task RunTestsAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            if (!messageBus.QueueMessage(new TestStarting(this, DisplayName)))
                cancellationTokenSource.Cancel();
            else
            {
                try
                {
                    lambda();

                    if (!messageBus.QueueMessage(new TestPassed(this, DisplayName, 0, null)))
                        cancellationTokenSource.Cancel();
                }
                catch (Exception ex)
                {
                    if (!messageBus.QueueMessage(new TestFailed(this, DisplayName, 0, null, ex)))
                        cancellationTokenSource.Cancel();
                }
            }

            if (!messageBus.QueueMessage(new TestFinished(this, DisplayName, 0, null)))
                cancellationTokenSource.Cancel();

            return Task.FromResult(0);
        }
 protected override async Task<RunSummary> RunTestAsync()
 {
     var test = new XunitTest(TestCase, DisplayName);
     var aggregator = new ExceptionAggregator(Aggregator);
     var runner = new XunitTestRunner(test, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, SkipReason, BeforeAfterAttributes, aggregator, CancellationTokenSource);
     return await KuduXunitTestRunnerUtils.RunTestAsync(runner, MessageBus, aggregator);
 }
 public virtual Task<RunSummary> RunAsync(SpecificationBase specification,
                                  IMessageBus messageBus,
                                  ExceptionAggregator aggregator,
                                  CancellationTokenSource cancellationTokenSource)
 {
     return new ObservationTestCaseRunner(specification, this, DisplayName, messageBus, aggregator, cancellationTokenSource).RunAsync();
 }
예제 #11
0
        public Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            var tcs = new TaskCompletionSource<RunSummary>();

            RunTestImpl(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource, tcs);

            return tcs.Task;
        }
 /// <inheritdoc />
 public override Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink,
                                           IMessageBus messageBus,
                                           object[] constructorArguments,
                                           ExceptionAggregator aggregator,
                                           CancellationTokenSource cancellationTokenSource)
 {
     return new XunitTheoryTestCaseRunner(this, DisplayName, SkipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource).RunAsync();
 }
예제 #13
0
 public FactDiscovererTests()
 {
     aggregator = new ExceptionAggregator();
     cancellationTokenSource = new CancellationTokenSource();
     factAttribute = Mocks.FactAttribute();
     messageBus = new SpyMessageBus();
     options = TestFrameworkOptions.ForDiscovery();
 }
예제 #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XunitTheoryTestCaseRunner"/> class.
 /// </summary>
 /// <param name="testCase">The test case to be run.</param>
 /// <param name="displayName">The display name of the test case.</param>
 /// <param name="skipReason">The skip reason, if the test is to be skipped.</param>
 /// <param name="constructorArguments">The arguments to be passed to the test class constructor.</param>
 /// <param name="messageBus">The message bus to report run status to.</param>
 /// <param name="aggregator">The exception aggregator used to run code and collect exceptions.</param>
 /// <param name="cancellationTokenSource">The task cancellation token source, used to cancel the test run.</param>
 public XunitTheoryTestCaseRunner(IXunitTestCase testCase,
                                  string displayName,
                                  string skipReason,
                                  object[] constructorArguments,
                                  IMessageBus messageBus,
                                  ExceptionAggregator aggregator,
                                  CancellationTokenSource cancellationTokenSource)
     : base(testCase, displayName, skipReason, constructorArguments, NoArguments, messageBus, aggregator, cancellationTokenSource) { }
 /// <inheritdoc />
 public override async Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
 {
     var messageBusInterceptor = new SkippableTestMessageBus(messageBus, this.SkippingExceptionNames);
     var result = await base.RunAsync(diagnosticMessageSink, messageBusInterceptor, constructorArguments, aggregator, cancellationTokenSource);
     result.Failed -= messageBusInterceptor.SkippedCount;
     result.Skipped += messageBusInterceptor.SkippedCount;
     return result;
 }
예제 #16
0
        public override Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            var sta = StaTaskScheduler.DefaultSta;
            var task = Task.Factory.StartNew(async () =>
            {
                Debug.Assert(sta.Threads.Length == 1);
                Debug.Assert(sta.Threads[0] == Thread.CurrentThread);

                using (await _wpfTestSerializationGate.DisposableWaitAsync())
                {
                    try
                    {
                        // Sync up FTAO to the context that we are creating here. 
                        ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = new ForegroundThreadData(
                            Thread.CurrentThread,
                            StaTaskScheduler.DefaultSta,
                            ForegroundThreadDataKind.StaUnitTest);

                        // All WPF Tests need a DispatcherSynchronizationContext and we dont want to block pending keyboard
                        // or mouse input from the user. So use background priority which is a single level below user input.
                        var dispatcherSynchronizationContext = new DispatcherSynchronizationContext();

                        // xUnit creates its own synchronization context and wraps any existing context so that messages are
                        // still pumped as necessary. So we are safe setting it here, where we are not safe setting it in test.
                        SynchronizationContext.SetSynchronizationContext(dispatcherSynchronizationContext);

                        // Just call back into the normal xUnit dispatch process now that we are on an STA Thread with no synchronization context.
                        var baseTask = base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource);
                        do
                        {
                            var delay = Task.Delay(TimeSpan.FromMilliseconds(10), cancellationTokenSource.Token);
                            var completed = await Task.WhenAny(baseTask, delay).ConfigureAwait(false);
                            if (completed == baseTask)
                            {
                                return await baseTask.ConfigureAwait(false);
                            }

                            // Schedule a task to pump messages on the UI thread.  
                            await Task.Factory.StartNew(
                                () => WaitHelper.WaitForDispatchedOperationsToComplete(DispatcherPriority.ApplicationIdle),
                                cancellationTokenSource.Token,
                                TaskCreationOptions.None,
                                sta).ConfigureAwait(false);
                        }
                        while (true);
                    }
                    finally
                    {
                        ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = null;

                        // Cleanup the synchronization context even if the test is failing exceptionally
                        SynchronizationContext.SetSynchronizationContext(null);
                    }
                }
            }, cancellationTokenSource.Token, TaskCreationOptions.None, sta);

            return task.Unwrap();
        }
		public VsixTestCollectionRunner (VsixTestCollection testCollection, IEnumerable<IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, 
			IMessageBus messageBus, ITestCaseOrderer testCaseOrderer, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) :
			base (testCollection, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource)
		{
			this.diagnosticMessageSink = diagnosticMessageSink;

			vsVersion = testCollection.VisualStudioVersion;
			rootSuffix = testCollection.RootSuffix;
			vs = new VsClient (vsVersion, Guid.NewGuid ().ToString (), rootSuffix);
		}
        private static async Task<RunSummary> RunTestAsyncCore(XunitTestRunner runner,
                                                               IMessageBus messageBus,
                                                               ExceptionAggregator aggregator,
                                                               bool disableRetry)
        {
            try
            {
                DelayedMessageBus delayedMessageBus = null;
                RunSummary summary = null;

                // First run
                if (!disableRetry)
                {
                    // This is really the only tricky bit: we need to capture and delay messages (since those will
                    // contain run status) until we know we've decided to accept the final result;
                    delayedMessageBus = new DelayedMessageBus(messageBus);

                    runner.SetMessageBus(delayedMessageBus);
                    summary = await RunTestInternalAsync(runner);

                    // if succeeded
                    if (summary.Failed == 0 || aggregator.HasExceptions)
                    {
                        delayedMessageBus.Flush(false);
                        return summary;
                    }
                }

                // Final run
                runner.SetMessageBus(new KuduTraceMessageBus(messageBus));
                summary = await RunTestInternalAsync(runner);

                // flush delay messages
                if (delayedMessageBus != null)
                {
                    delayedMessageBus.Flush(summary.Failed == 0 && !aggregator.HasExceptions);
                }

                return summary;
            }
            catch (Exception ex)
            {
                // this is catastrophic
                messageBus.QueueMessage(new TestFailed(runner.GetTest(), 0, null, ex));

                return new RunSummary { Failed = 1, Total = 1 };
            }
            finally
            {
                // set to original
                runner.SetMessageBus(messageBus);
            }
        }
 public BenchmarkTestInvoker(ITest test,
                         IMessageBus messageBus,
                         Type testClass,
                         object[] constructorArguments,
                         MethodInfo testMethod,
                         object[] testMethodArguments,
                         IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes,
                         ExceptionAggregator aggregator,
                         CancellationTokenSource cancellationTokenSource)
     : base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, beforeAfterAttributes, aggregator, cancellationTokenSource)
 {
 }
 public static Task<RunSummary> RunTestAsync(XunitTestRunner runner,
                                             IMessageBus messageBus,
                                             ExceptionAggregator aggregator)
 {
     // fork non-SynchronizationContext thread
     var result = Task.Factory.StartNew(
                     () => RunTestAsyncCore(runner, messageBus, aggregator).Result,
                     new CancellationToken(),
                     TaskCreationOptions.None,
                     TaskScheduler.Default).Result;
     return Task.FromResult(result);
 }
 protected override async Task<RunSummary> RunTestAsync()
 {
     var test = new XunitTest(TestCase, DisplayName);
     var aggregator = new ExceptionAggregator(Aggregator);
     var disableRetry = ((KuduXunitTestCase)TestCase).DisableRetry;
     if (!disableRetry)
     {
         var value = ConfigurationManager.AppSettings["DisableRetry"];
         disableRetry = string.IsNullOrEmpty(value) || bool.Parse(value);
     }
     var runner = new XunitTestRunner(test, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, SkipReason, BeforeAfterAttributes, aggregator, CancellationTokenSource);
     return await KuduXunitTestRunnerUtils.RunTestAsync(runner, MessageBus, aggregator, disableRetry);
 }
		public TestCollectionRunner(Dictionary<Type, object> assemblyFixtureMappings,
			ITestCollection testCollection,
			IEnumerable<IXunitTestCase> testCases,
			IMessageSink diagnosticMessageSink,
			IMessageBus messageBus,
			ITestCaseOrderer testCaseOrderer,
			ExceptionAggregator aggregator,
			CancellationTokenSource cancellationTokenSource)
			: base(testCollection, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource)
		{
			this.assemblyFixtureMappings = assemblyFixtureMappings;
			this.diagnosticMessageSink = diagnosticMessageSink;
		}
예제 #23
0
        public async override Task<RunSummary> RunAsync(
            IMessageSink diagnosticMessageSink,
            IMessageBus messageBus,
            object[] constructorArguments,
            ExceptionAggregator aggregator,
            CancellationTokenSource cancellationTokenSource)
        {
            // warm up
            await base.RunAsync(NullMessageSink.Instance, NullMessageBus.Instance, constructorArguments, aggregator, cancellationTokenSource);

            var performanceMessageBus = new PerformanceMessageBus(messageBus);
            return await base.RunAsync(diagnosticMessageSink, performanceMessageBus, constructorArguments, aggregator, cancellationTokenSource);
        }
예제 #24
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="UITestCaseRunner" /> class.
 /// </summary>
 /// <param name="testCase">The test case to be run.</param>
 /// <param name="displayName">The display name of the test case.</param>
 /// <param name="skipReason">The skip reason, if the test is to be skipped.</param>
 /// <param name="constructorArguments">The arguments to be passed to the test class constructor.</param>
 /// <param name="testMethodArguments">The arguments to be passed to the test method.</param>
 /// <param name="messageBus">The message bus to report run status to.</param>
 /// <param name="aggregator">The exception aggregator used to run code and collect exceptions.</param>
 /// <param name="cancellationTokenSource">The task cancellation token source, used to cancel the test run.</param>
 /// <param name="syncContextAdapter">The <see cref="SynchronizationContext" /> adapter to use.</param>
 internal UITestCaseRunner(
     IXunitTestCase testCase,
     string displayName,
     string skipReason,
     object[] constructorArguments,
     object[] testMethodArguments,
     IMessageBus messageBus,
     ExceptionAggregator aggregator,
     CancellationTokenSource cancellationTokenSource,
     SyncContextAdapter syncContextAdapter)
     : base(testCase, displayName, skipReason, constructorArguments, testMethodArguments, messageBus, aggregator, cancellationTokenSource) {
     this.syncContextAdapter = syncContextAdapter;
 }
예제 #25
0
        public override Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
            => Task.Factory.StartNew(() =>
            {
                // All of the tests which use StaTestCase require an STA thread, so assert that we are 
                // actually running from an STA Thread (which should be from the StaTaskScheduler pool).
                Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA);

                // xUnit will set and clean up its own context, so assert that none is currently
                // set. If our assert fails, then something wasn't cleaned up properly...
                Debug.Assert(SynchronizationContext.Current == null);

                // Just call back into the normal xUnit dispatch process now that we are on an STA Thread with no synchronization context.
                return base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource).Result;
            }, CancellationToken.None, TaskCreationOptions.None, StaTaskScheduler.DefaultSta);
예제 #26
0
    public static void BeforeAfterTestAttributesComeFromBothTestClassAndTestMethod()
    {
        var testCase = Mocks.XunitTestCase<ClassUnderTest>("Passing");
        var messageBus = Substitute.For<IMessageBus>();
        var aggregator = new ExceptionAggregator();
        var tokenSource = new CancellationTokenSource();

        var runner = new XunitTestCaseRunner(testCase, "Display Name", "Skip Reason", new object[0], new object[0], messageBus, aggregator, tokenSource);

        Assert.Collection(runner.BeforeAfterAttributes,
            attr => Assert.IsType<BeforeAfterOnClass>(attr),
            attr => Assert.IsType<BeforeAfterOnMethod>(attr)
        );
    }
		public VsixTestClassRunner (IVsClient vsClient,
								   ITestClass testClass,
								   IReflectionTypeInfo @class,
								   IEnumerable<IXunitTestCase> testCases,
								   IMessageSink diagnosticMessageSink,
								   IMessageBus messageBus,
								   ITestCaseOrderer testCaseOrderer,
								   ExceptionAggregator aggregator,
								   CancellationTokenSource cancellationTokenSource,
								   IDictionary<Type, object> collectionFixtureMappings)
			: base (testClass, @class, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings)
		{
			this.vsClient = vsClient;
		}
예제 #28
0
        public StepInvoker(
            IStep step,
            Func<IStepContext, Task> body,
            ExceptionAggregator aggregator,
            CancellationTokenSource cancellationTokenSource)
        {
            Guard.AgainstNullArgument("aggregator", aggregator);
            Guard.AgainstNullArgument("cancellationTokenSource", cancellationTokenSource);

            this.step = step;
            this.body = body;
            this.aggregator = aggregator;
            this.cancellationTokenSource = cancellationTokenSource;
        }
 public BenchmarkTestCaseRunner(IXunitTestCase testCase,
                                  string displayName,
                                  string skipReason,
                                  object[] constructorArguments,
                                  object[] testMethodArguments,
                                  IMessageSink diagnosticMessageSink,
                                  IMessageBus messageBus,
                                  ExceptionAggregator aggregator,
                                  CancellationTokenSource cancellationTokenSource)
     : base(testCase, displayName, skipReason, constructorArguments, testMethodArguments, messageBus, aggregator, cancellationTokenSource)
 {
     _diagnosticMessageSink = diagnosticMessageSink;
     _discoverArguments = testMethodArguments == null;
 }
        public TestCaseRunnerWithMethodFixtures(IXunitTestCase testCase, string displayName, string skipReason, object[] constructorArguments, object[] testMethodArguments, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
            : base(testCase, displayName, skipReason, constructorArguments, testMethodArguments, messageBus, aggregator, cancellationTokenSource) {

            for (var i = 0; i < constructorArguments.Length; i++) {
                var methodFixture = constructorArguments[i] as IMethodFixture;
                if (methodFixture == null) {
                    continue;
                }

                var methodFixtureType = methodFixture.GetType();
                methodFixture = (IMethodFixture)Activator.CreateInstance(methodFixtureType);
                constructorArguments[i] = methodFixture;
                _methodFixtures.Add(methodFixture);
            }
        }
        /// <inheritdoc/>
        public override Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            diagnosticMessageSink.OnMessage(new DiagnosticMessage("[{0}] Running {1}", this.GetType( ).GetDescriptiveName( ), this.DisplayName));

            return(base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource));
        }
예제 #32
0
        /// <inheritdoc/>
        protected override Task RunTestsAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            if (!messageBus.QueueMessage(new TestStarting(this, DisplayName)))
            {
                cancellationTokenSource.Cancel();
            }
            else
            {
                try
                {
                    lambda();

                    if (!messageBus.QueueMessage(new TestPassed(this, DisplayName, 0, null)))
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
                catch (Exception ex)
                {
                    if (!messageBus.QueueMessage(new TestFailed(this, DisplayName, 0, null, ex)))
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
            }

            if (!messageBus.QueueMessage(new TestFinished(this, DisplayName, 0, null)))
            {
                cancellationTokenSource.Cancel();
            }

            return(Task.FromResult(0));
        }
예제 #33
0
 public override Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
 => new UITheoryTestCaseRunner(this, this.DisplayName, this.SkipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource).RunAsync();
예제 #34
0
        /// <summary>
        /// Adds exceptions from another aggregator into this aggregator.
        /// </summary>
        /// <param name="aggregator">The aggregator whose exceptions should be copied.</param>
        public void Aggregate(ExceptionAggregator aggregator)
        {
            Guard.ArgumentNotNull(nameof(aggregator), aggregator);

            exceptions.AddRange(aggregator.exceptions);
        }
예제 #35
0
 protected override Task <decimal> InvokeTestMethodAsync(ExceptionAggregator aggregator)
 {
     return(new UITestInvoker(this.Test, this.MessageBus, this.TestClass, this.ConstructorArguments, this.TestMethod, this.TestMethodArguments, this.BeforeAfterAttributes, aggregator, this.CancellationTokenSource, this.threadRental).RunAsync());
 }
예제 #36
0
 internal UITestRunner(ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, string skipReason, IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, ThreadRental threadRental)
     : base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, aggregator, cancellationTokenSource)
 {
     this.threadRental = threadRental;
 }
예제 #37
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UITestInvoker"/> class.
 /// </summary>
 internal UITestInvoker(ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, SyncContextAdapter syncContextAdapter)
     : base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, beforeAfterAttributes, aggregator, cancellationTokenSource)
 {
     this.adapter = syncContextAdapter;
 }
예제 #38
0
        static void CreateFixture(Type interfaceType, ExceptionAggregator aggregator, Dictionary <Type, object> mappings)
        {
            var fixtureType = interfaceType.GetGenericArguments().Single();

            aggregator.Run(() => mappings[fixtureType] = Activator.CreateInstance(fixtureType));
        }
 public WinFormsTheoryTestCaseRunner(IXunitTestCase testCase, string displayName, string skipReason, object[] constructorArguments, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
     : base(testCase, displayName, skipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource)
 {
 }
            /// <inheritdoc />
            public override async Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
            {
                var messageBusInterceptor = new SkippableTestMessageBus(messageBus, this.SkippingExceptionNames);
                var result = await base.RunAsync(diagnosticMessageSink, messageBusInterceptor, constructorArguments, aggregator, cancellationTokenSource);

                result.Failed  -= messageBusInterceptor.SkippedCount;
                result.Skipped += messageBusInterceptor.SkippedCount;
                return(result);
            }
예제 #41
0
파일: TestRunner.cs 프로젝트: zabulus/xunit
        /// <summary>
        /// Runs the test.
        /// </summary>
        /// <returns>Returns summary information about the test that was run.</returns>
        public async Task <RunSummary> RunAsync()
        {
            var runSummary = new RunSummary {
                Total = 1
            };
            var output = string.Empty;

            if (!MessageBus.QueueMessage(new TestStarting(Test)))
            {
                CancellationTokenSource.Cancel();
            }
            else
            {
                AfterTestStarting();

                if (!string.IsNullOrEmpty(SkipReason))
                {
                    runSummary.Skipped++;

                    if (!MessageBus.QueueMessage(new TestSkipped(Test, SkipReason)))
                    {
                        CancellationTokenSource.Cancel();
                    }
                }
                else
                {
                    var aggregator = new ExceptionAggregator(Aggregator);

                    if (!aggregator.HasExceptions)
                    {
                        var tuple = await aggregator.RunAsync(() => InvokeTestAsync(aggregator));

                        runSummary.Time = tuple.Item1;
                        output          = tuple.Item2;
                    }

                    var exception = aggregator.ToException();
                    TestResultMessage testResult;

                    if (exception == null)
                    {
                        testResult = new TestPassed(Test, runSummary.Time, output);
                    }
                    else
                    {
                        testResult = new TestFailed(Test, runSummary.Time, output, exception);
                        runSummary.Failed++;
                    }

                    if (!CancellationTokenSource.IsCancellationRequested)
                    {
                        if (!MessageBus.QueueMessage(testResult))
                        {
                            CancellationTokenSource.Cancel();
                        }
                    }
                }

                Aggregator.Clear();
                BeforeTestFinished();

                if (Aggregator.HasExceptions)
                {
                    if (!MessageBus.QueueMessage(new TestCleanupFailure(Test, Aggregator.ToException())))
                    {
                        CancellationTokenSource.Cancel();
                    }
                }
            }

            if (!MessageBus.QueueMessage(new TestFinished(Test, runSummary.Time, output)))
            {
                CancellationTokenSource.Cancel();
            }

            return(runSummary);
        }
 /// <inheritdoc/>
 public override Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink,
                                            IMessageBus messageBus,
                                            object[] constructorArguments,
                                            ExceptionAggregator aggregator,
                                            CancellationTokenSource cancellationTokenSource)
 => new ExecutionErrorTestCaseRunner(this, messageBus, aggregator, cancellationTokenSource).RunAsync();
예제 #43
0
 /// <inheritdoc/>
 public virtual Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink,
                                           IMessageBus messageBus,
                                           object[] constructorArguments,
                                           ExceptionAggregator aggregator,
                                           CancellationTokenSource cancellationTokenSource)
 => new XunitTestCaseRunner(this, DisplayName, SkipReason, constructorArguments, TestMethodArguments, messageBus, aggregator, cancellationTokenSource).RunAsync();
예제 #44
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);
        }
        /// <inheritdoc/>
        public override Task <RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            // If test is not using STA or Timeout, return base result.
            if (!this.UseStaThread && this.Timeout < 1)
            {
                return(base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource));
            }

            return(this.TimeoutRunAsync(messageBus, cancellationTokenSource, () => base.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator, cancellationTokenSource)));
        }
예제 #46
0
        /// <summary>
        /// Run the tests in the test case.
        /// </summary>
        /// <param name="messageBus">The message bus to send 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>
        protected virtual Task RunTestsAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
        {
            var classUnderTest        = GetRuntimeClass();
            var methodUnderTest       = GetRuntimeMethod(classUnderTest);
            var beforeAfterAttributes = GetBeforeAfterAttributes(classUnderTest, methodUnderTest).ToList();

            return(RunTestsOnMethodAsync(messageBus, classUnderTest, constructorArguments, methodUnderTest, beforeAfterAttributes, aggregator, cancellationTokenSource));
        }
예제 #47
0
        /// <inheritdoc/>
        protected override async Task <RunSummary> RunTestAsync()
        {
            var testRunners = new List <XunitTestRunner>();
            var toDispose   = new List <IDisposable>();

            try
            {
                var dataAttributes = TestCase.TestMethod.Method.GetCustomAttributes(typeof(DataAttribute));

                foreach (var dataAttribute in dataAttributes)
                {
                    var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                    var args           = discovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                    var discovererType = Reflector.GetType(args[1], args[0]);
                    var discoverer     = ExtensibilityPointFactory.GetDataDiscoverer(discovererType);

                    foreach (var dataRow in discoverer.GetData(dataAttribute, TestCase.TestMethod.Method))
                    {
                        toDispose.AddRange(dataRow.OfType <IDisposable>());

                        ITypeInfo[] resolvedTypes = null;
                        var         methodToRun   = TestMethod;

                        if (methodToRun.IsGenericMethodDefinition)
                        {
                            resolvedTypes = TypeUtility.ResolveGenericTypes(TestCase.TestMethod.Method, dataRow);
                            methodToRun   = methodToRun.MakeGenericMethod(resolvedTypes.Select(t => ((IReflectionTypeInfo)t).Type).ToArray());
                        }

                        var parameterTypes    = methodToRun.GetParameters().Select(p => p.ParameterType).ToArray();
                        var convertedDataRow  = Reflector.ConvertArguments(dataRow, parameterTypes);
                        var theoryDisplayName = TypeUtility.GetDisplayNameWithArguments(TestCase.TestMethod.Method, DisplayName, convertedDataRow, resolvedTypes);

                        testRunners.Add(new XunitTestRunner(TestCase, MessageBus, TestClass, ConstructorArguments, methodToRun, convertedDataRow, theoryDisplayName, SkipReason, BeforeAfterAttributes, Aggregator, CancellationTokenSource));
                    }
                }
            }
            catch (Exception ex)
            {
                if (!MessageBus.QueueMessage(new TestStarting(TestCase, DisplayName)))
                {
                    CancellationTokenSource.Cancel();
                }
                else
                {
                    if (!MessageBus.QueueMessage(new TestFailed(TestCase, DisplayName, 0, null, ex.Unwrap())))
                    {
                        CancellationTokenSource.Cancel();
                    }
                }

                if (!MessageBus.QueueMessage(new TestFinished(TestCase, DisplayName, 0, null)))
                {
                    CancellationTokenSource.Cancel();
                }

                return(new RunSummary {
                    Total = 1, Failed = 1
                });
            }

            var runSummary = new RunSummary();

            foreach (var testRunner in testRunners)
            {
                runSummary.Aggregate(await testRunner.RunAsync());
            }

            var timer      = new ExecutionTimer();
            var aggregator = new ExceptionAggregator();

            // REVIEW: What should be done with these leftover errors?

            foreach (var disposable in toDispose)
            {
                timer.Aggregate(() => aggregator.Run(() => disposable.Dispose()));
            }

            runSummary.Time += timer.Total;

            return(runSummary);
        }
예제 #48
0
        /// <summary>
        /// Runs a single test for a given test method.
        /// </summary>
        /// <param name="messageBus">The message bus to send results to.</param>
        /// <param name="classUnderTest">The class under test.</param>
        /// <param name="constructorArguments">The arguments to pass to the constructor.</param>
        /// <param name="methodUnderTest">The method under test.</param>
        /// <param name="testMethodArguments">The arguments to pass to the test method.</param>
        /// <param name="displayName">The display name for the test.</param>
        /// <param name="beforeAfterAttributes">The <see cref="BeforeAfterTestAttribute"/> instances attached to the test.</param>
        /// <param name="parentAggregator">The parent aggregator that contains the exceptions up to this point.</param>
        /// <param name="cancellationTokenSource">The cancellation token source that indicates whether cancellation has been requested.</param>
        protected async Task <decimal> RunTestWithArgumentsAsync(IMessageBus messageBus,
                                                                 Type classUnderTest,
                                                                 object[] constructorArguments,
                                                                 MethodInfo methodUnderTest,
                                                                 object[] testMethodArguments,
                                                                 string displayName,
                                                                 List <BeforeAfterTestAttribute> beforeAfterAttributes,
                                                                 ExceptionAggregator parentAggregator,
                                                                 CancellationTokenSource cancellationTokenSource)
        {
            var executionTimeInSeconds = 0.0m;
            var aggregator             = new ExceptionAggregator(parentAggregator);
            var output = String.Empty;  // TODO: Add output facilities for v2

            if (!messageBus.QueueMessage(new TestStarting(this, displayName)))
            {
                cancellationTokenSource.Cancel();
            }
            else
            {
                if (!String.IsNullOrEmpty(SkipReason))
                {
                    if (!messageBus.QueueMessage(new TestSkipped(this, displayName, SkipReason)))
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
                else
                {
                    var beforeAttributesRun = new Stack <BeforeAfterTestAttribute>();
                    var executionTime       = new ExecutionTime();

                    if (!aggregator.HasExceptions)
                    {
                        await aggregator.RunAsync(async() =>
                        {
                            object testClass = null;

                            if (!methodUnderTest.IsStatic)
                            {
                                if (!messageBus.QueueMessage(new TestClassConstructionStarting(this, displayName)))
                                {
                                    cancellationTokenSource.Cancel();
                                }

                                try
                                {
                                    if (!cancellationTokenSource.IsCancellationRequested)
                                    {
                                        executionTime.Aggregate(() => testClass = Activator.CreateInstance(classUnderTest, constructorArguments));
                                    }
                                }
                                finally
                                {
                                    if (!messageBus.QueueMessage(new TestClassConstructionFinished(this, displayName)))
                                    {
                                        cancellationTokenSource.Cancel();
                                    }
                                }
                            }

                            if (!cancellationTokenSource.IsCancellationRequested)
                            {
                                await aggregator.RunAsync(async() =>
                                {
                                    foreach (var beforeAfterAttribute in beforeAfterAttributes)
                                    {
                                        var attributeName = beforeAfterAttribute.GetType().Name;
                                        if (!messageBus.QueueMessage(new BeforeTestStarting(this, displayName, attributeName)))
                                        {
                                            cancellationTokenSource.Cancel();
                                        }
                                        else
                                        {
                                            try
                                            {
                                                executionTime.Aggregate(() => beforeAfterAttribute.Before(methodUnderTest));
                                                beforeAttributesRun.Push(beforeAfterAttribute);
                                            }
                                            finally
                                            {
                                                if (!messageBus.QueueMessage(new BeforeTestFinished(this, displayName, attributeName)))
                                                {
                                                    cancellationTokenSource.Cancel();
                                                }
                                            }
                                        }

                                        if (cancellationTokenSource.IsCancellationRequested)
                                        {
                                            return;
                                        }
                                    }

                                    if (!cancellationTokenSource.IsCancellationRequested)
                                    {
                                        var parameterTypes = methodUnderTest.GetParameters().Select(p => p.ParameterType).ToArray();
                                        var oldSyncContext = SynchronizationContext.Current;

                                        try
                                        {
                                            var asyncSyncContext = new AsyncTestSyncContext();
                                            SetSynchronizationContext(asyncSyncContext);

                                            await aggregator.RunAsync(async() =>
                                            {
                                                await executionTime.AggregateAsync(async() =>
                                                {
                                                    var result = methodUnderTest.Invoke(testClass, Reflector.ConvertArguments(testMethodArguments, parameterTypes));
                                                    var task   = result as Task;
                                                    if (task != null)
                                                    {
                                                        await task;
                                                    }
                                                    else
                                                    {
                                                        var ex = await asyncSyncContext.WaitForCompletionAsync();
                                                        if (ex != null)
                                                        {
                                                            aggregator.Add(ex);
                                                        }
                                                    }
                                                });
                                            });
                                        }
                                        finally
                                        {
                                            SetSynchronizationContext(oldSyncContext);
                                        }
                                    }
                                });

                                foreach (var beforeAfterAttribute in beforeAttributesRun)
                                {
                                    var attributeName = beforeAfterAttribute.GetType().Name;
                                    if (!messageBus.QueueMessage(new AfterTestStarting(this, displayName, attributeName)))
                                    {
                                        cancellationTokenSource.Cancel();
                                    }

                                    aggregator.Run(() => executionTime.Aggregate(() => beforeAfterAttribute.After(methodUnderTest)));

                                    if (!messageBus.QueueMessage(new AfterTestFinished(this, displayName, attributeName)))
                                    {
                                        cancellationTokenSource.Cancel();
                                    }
                                }
                            }

                            aggregator.Run(() =>
                            {
                                var disposable = testClass as IDisposable;
                                if (disposable != null)
                                {
                                    if (!messageBus.QueueMessage(new TestClassDisposeStarting(this, displayName)))
                                    {
                                        cancellationTokenSource.Cancel();
                                    }

                                    try
                                    {
                                        executionTime.Aggregate(disposable.Dispose);
                                    }
                                    finally
                                    {
                                        if (!messageBus.QueueMessage(new TestClassDisposeFinished(this, displayName)))
                                        {
                                            cancellationTokenSource.Cancel();
                                        }
                                    }
                                }
                            });
                        });
                    }

                    if (!cancellationTokenSource.IsCancellationRequested)
                    {
                        executionTimeInSeconds = (decimal)executionTime.Total.TotalSeconds;

                        var exception  = aggregator.ToException();
                        var testResult = exception == null ? (TestResultMessage) new TestPassed(this, displayName, executionTimeInSeconds, output)
                                                           : new TestFailed(this, displayName, executionTimeInSeconds, output, exception);
                        if (!messageBus.QueueMessage(testResult))
                        {
                            cancellationTokenSource.Cancel();
                        }
                    }
                }
            }

            if (!messageBus.QueueMessage(new TestFinished(this, displayName, executionTimeInSeconds, output)))
            {
                cancellationTokenSource.Cancel();
            }

            return(executionTimeInSeconds);
        }
예제 #49
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionAggregator"/> class that
        /// contains the exception list of its parent.
        /// </summary>
        /// <param name="parent">The parent aggregator to copy exceptions from.</param>
        public ExceptionAggregator(ExceptionAggregator parent)
        {
            Guard.ArgumentNotNull(nameof(parent), parent);

            exceptions = new List <Exception>(parent.exceptions);
        }
 internal UITheoryTestCaseRunner(UITheoryTestCase testCase, string displayName, string skipReason, object[] constructorArguments, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, ThreadRental threadRental)
     : base(testCase, displayName, skipReason, constructorArguments, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource)
 {
     this.threadRental = threadRental;
 }
예제 #51
0
 internal void SetExceptionAggregator(ExceptionAggregator aggregator)
 {
     this.aggregator = aggregator;
 }
예제 #52
0
파일: TestRunner.cs 프로젝트: zabulus/xunit
 /// <summary>
 /// Override this method to invoke the test.
 /// </summary>
 /// <param name="aggregator">The exception aggregator used to run code and collect exceptions.</param>
 /// <returns>Returns a tuple which includes the execution time (in seconds) spent running the
 /// test method, and any output that was returned by the test.</returns>
 protected abstract Task <Tuple <decimal, string> > InvokeTestAsync(ExceptionAggregator aggregator);
예제 #53
0
 protected override XunitTestRunner CreateTestRunner(ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, string skipReason, IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
 => new RazorTestRunner(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, new ExceptionAggregator(aggregator), cancellationTokenSource);
예제 #54
0
 /// <inheritdoc />
 public override Task <RunSummary> RunAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
 {
     return(new XunitTheoryTestCaseRunner(this, DisplayName, SkipReason, constructorArguments, messageBus, aggregator, cancellationTokenSource).RunAsync());
 }
예제 #55
0
 /// <inheritdoc/>
 protected override Task <decimal> InvokeTestAsync(ExceptionAggregator aggregator)
 {
     return(new XunitTestInvoker(TestCase, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, beforeAfterAttributes, DisplayName, aggregator, CancellationTokenSource).RunAsync());
 }
예제 #56
0
 public RazorTestInvoker(Func <ITestOutputHelper> testOutputHelperFactory, ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
     : base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, beforeAfterAttributes, aggregator, cancellationTokenSource)
 {
     _testOutputHelperFactory = testOutputHelperFactory;
 }
예제 #57
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExceptionAggregator"/> class that
 /// contains the exception list of its parent.
 /// </summary>
 /// <param name="parent">The parent aggregator to copy exceptions from.</param>
 public ExceptionAggregator(ExceptionAggregator parent)
 {
     exceptions = new List <Exception>(parent.exceptions);
 }
예제 #58
0
        private RunSummary RunTestCollection(IMessageBus messageBus,
                                             ITestCollection collection,
                                             IEnumerable <XunitTestCase> testCases,
                                             ITestCaseOrderer orderer,
                                             CancellationTokenSource cancellationTokenSource)
        {
            var collectionSummary         = new RunSummary();
            var collectionFixtureMappings = new Dictionary <Type, object>();
            var aggregator = new ExceptionAggregator();

            if (collection.CollectionDefinition != null)
            {
                var declarationType = ((IReflectionTypeInfo)collection.CollectionDefinition).Type;
                foreach (var interfaceType in declarationType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollectionFixture <>)))
                {
                    CreateFixture(interfaceType, aggregator, collectionFixtureMappings);
                }

                var ordererAttribute = collection.CollectionDefinition.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();
                if (ordererAttribute != null)
                {
                    orderer = GetTestCaseOrderer(ordererAttribute);
                }
            }

            if (messageBus.QueueMessage(new TestCollectionStarting(collection)))
            {
                foreach (var testCasesByClass in testCases.GroupBy(tc => tc.Class))
                {
                    var classSummary = new RunSummary();

                    if (!messageBus.QueueMessage(new TestClassStarting(collection, testCasesByClass.Key.Name)))
                    {
                        cancellationTokenSource.Cancel();
                    }
                    else
                    {
                        RunTestClass(messageBus, collection, collectionFixtureMappings, (IReflectionTypeInfo)testCasesByClass.Key, testCasesByClass, orderer, classSummary, aggregator, cancellationTokenSource);
                        collectionSummary.Aggregate(classSummary);
                    }

                    if (!messageBus.QueueMessage(new TestClassFinished(collection, testCasesByClass.Key.Name, classSummary.Time, classSummary.Total, classSummary.Failed, classSummary.Skipped)))
                    {
                        cancellationTokenSource.Cancel();
                    }

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

            foreach (var fixture in collectionFixtureMappings.Values.OfType <IDisposable>())
            {
                try
                {
                    fixture.Dispose();
                }
                catch (Exception ex)
                {
                    if (!messageBus.QueueMessage(new ErrorMessage(ex.Unwrap())))
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
            }

            messageBus.QueueMessage(new TestCollectionFinished(collection, collectionSummary.Time, collectionSummary.Total, collectionSummary.Failed, collectionSummary.Skipped));
            return(collectionSummary);
        }
 protected override XunitTestRunner CreateTestRunner(ITest test, IMessageBus messageBus, Type testClass, object[] constructorArguments, MethodInfo testMethod, object[] testMethodArguments, string skipReason, IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
 => new UITestRunner(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, aggregator, cancellationTokenSource, WinFormsSynchronizationContextAdapter.Default);
예제 #60
0
        private static void RunTestClass(IMessageBus messageBus,
                                         ITestCollection collection,
                                         Dictionary <Type, object> collectionFixtureMappings,
                                         IReflectionTypeInfo testClass,
                                         IEnumerable <XunitTestCase> testCases,
                                         ITestCaseOrderer orderer,
                                         RunSummary classSummary,
                                         ExceptionAggregator aggregator,
                                         CancellationTokenSource cancellationTokenSource)
        {
            var testClassType        = testClass.Type;
            var fixtureMappings      = new Dictionary <Type, object>();
            var constructorArguments = new List <object>();

            var ordererAttribute = testClass.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();

            if (ordererAttribute != null)
            {
                orderer = GetTestCaseOrderer(ordererAttribute);
            }

            if (testClassType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollectionFixture <>)))
            {
                aggregator.Add(new TestClassException("A test class may not be decorated with ICollectionFixture<> (decorate the test collection class instead)."));
            }

            foreach (var interfaceType in testClassType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IClassFixture <>)))
            {
                CreateFixture(interfaceType, aggregator, fixtureMappings);
            }

            if (collection.CollectionDefinition != null)
            {
                var declarationType = ((IReflectionTypeInfo)collection.CollectionDefinition).Type;
                foreach (var interfaceType in declarationType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IClassFixture <>)))
                {
                    CreateFixture(interfaceType, aggregator, fixtureMappings);
                }
            }

            var isStaticClass = testClassType.IsAbstract && testClassType.IsSealed;

            if (!isStaticClass)
            {
                var ctors = testClassType.GetConstructors();
                if (ctors.Length != 1)
                {
                    aggregator.Add(new TestClassException("A test class may only define a single public constructor."));
                }
                else
                {
                    var ctor            = ctors.Single();
                    var unusedArguments = new List <string>();

                    foreach (var paramInfo in ctor.GetParameters())
                    {
                        object fixture;

                        if (fixtureMappings.TryGetValue(paramInfo.ParameterType, out fixture) || collectionFixtureMappings.TryGetValue(paramInfo.ParameterType, out fixture))
                        {
                            constructorArguments.Add(fixture);
                        }
                        else
                        {
                            unusedArguments.Add(String.Format("{0} {1}", paramInfo.ParameterType.Name, paramInfo.Name));
                        }
                    }

                    if (unusedArguments.Count > 0)
                    {
                        aggregator.Add(new TestClassException("The following constructor arguments did not have matching fixture data: " + String.Join(", ", unusedArguments)));
                    }
                }
            }

            var orderedTestCases = orderer.OrderTestCases(testCases);
            var methodGroups     = orderedTestCases.GroupBy(tc => tc.Method);

            foreach (var method in methodGroups)
            {
                if (!messageBus.QueueMessage(new TestMethodStarting(collection, testClass.Name, method.Key.Name)))
                {
                    cancellationTokenSource.Cancel();
                }
                else
                {
                    RunTestMethod(messageBus, constructorArguments.ToArray(), method, classSummary, aggregator, cancellationTokenSource);
                }

                if (!messageBus.QueueMessage(new TestMethodFinished(collection, testClass.Name, method.Key.Name)))
                {
                    cancellationTokenSource.Cancel();
                }

                if (cancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
            }

            foreach (var fixture in fixtureMappings.Values.OfType <IDisposable>())
            {
                try
                {
                    fixture.Dispose();
                }
                catch (Exception ex)
                {
                    if (!messageBus.QueueMessage(new ErrorMessage(ex.Unwrap())))
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
            }
        }