Example #1
0
        protected override async Task <RunSummary> RunTestClassAsync(
            ITestClass testClass,
            IReflectionTypeInfo @class,
            IEnumerable <IXunitTestCase> testCases)
        {
            // Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
            // so instead we'll just let collection fixtures override assembly fixtures.
            var combinedFixtures = new Dictionary <Type, object>(_assemblyFixtureMappings);

            foreach (var(key, value) in CollectionFixtureMappings)
            {
                combinedFixtures[key] = value;
            }

            // We've done everything we need, so let the built-in types do the rest of the heavy lifting
            var runner = new XunitTestClassRunner(
                testClass,
                @class,
                testCases,
                _diagnosticMessageSink,
                MessageBus,
                TestCaseOrderer,
                new ExceptionAggregator(Aggregator),
                CancellationTokenSource,
                combinedFixtures
                );

            var runSummary = await runner.RunAsync();

            return(runSummary);
        }
Example #2
0
        protected MethodInfo SelectTestClassFixtureInjectionMethod(IReflectionTypeInfo Class)
        {
            var methods = Class
                          .Type
                          .GetTypeInfo()
                          .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                          .Where(ci => !ci.IsGenericMethod && ci.Name == "SetFixture")
                          .ToList();

            var methodGroup = methods
                              .GroupBy(ci => ci.GetParameters().Length)
                              .OrderByDescending(g => g.Count())
                              .FirstOrDefault();

            if (methodGroup == null)
            {
                return(null);
            }
            if (methodGroup.Count() > 1)
            {
                Aggregator.Add(
                    new TestClassException(
                        $"Ambiguous SetFixture method on test class '{Class.Type.FullName}'. " +
                        "Remember that the overload with more parameters is selected."
                        )
                    );
                return(null);
            }
            return(methods
                   .OrderByDescending(ci => ci.GetParameters().Length)
                   .FirstOrDefault());
        }
Example #3
0
        /// <inheritdoc/>
        protected void SetTestCaseOrderer(IReflectionTypeInfo Class)
        {
            var ordererAttribute = Class.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();

            if (ordererAttribute != null)
            {
                try
                {
                    var testCaseOrderer = ExtensibilityPointFactory.GetTestCaseOrderer(diagnosticMessageSink, ordererAttribute);
                    if (testCaseOrderer != null)
                    {
                        TestCaseOrderer = testCaseOrderer;
                    }
                    else
                    {
                        var args = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                        diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Could not find type '{args[0]}' in {args[1]} for class-level test case orderer on test class '{Class.Name}'"));
                    }
                }
                catch (Exception ex)
                {
                    var innerEx = ex.InnerException ?? ex; //.Unwrap();
                    var args    = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                    diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Class-level test case orderer '{args[0]}' for test class '{Class.Name}' threw '{innerEx.GetType().FullName}' " +
                                                                          $"during construction: {innerEx.Message}{Environment.NewLine}{innerEx.StackTrace}"));
                }
            }
        }
Example #4
0
        /// <nodoc />
        public ClassRunner(
            Logger logger,
            ITestClass testClass,
            IReflectionTypeInfo typeInfo,
            IEnumerable <IXunitTestCase> testCases,
            IMessageSink diagnosticMessageSink,
            IMessageBus messageBus,
            ITestCaseOrderer testCaseOrderer,
            ExceptionAggregator aggregator,
            CancellationTokenSource cancellationTokenSource,
            IDictionary <Type, object> collectionFixtureMappings)
            : base(testClass, typeInfo, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator,
                   cancellationTokenSource, collectionFixtureMappings)
        {
            m_logger = logger;

            // Check if the test class has the attribute that specifies system requirements to run the tests
            var testClassAttributes = typeInfo.Type.GetCustomAttributes(typeof(TestClassIfSupportedAttribute), inherit: true);

            // If any of the attributes specify Skip, the test class is skipped
            m_skipTestClassSkipReason = testClassAttributes
                                        .Cast <TestClassIfSupportedAttribute>()
                                        .Select(attribute => attribute.Skip)
                                        .Where(skipReason => !string.IsNullOrEmpty(skipReason))
                                        .FirstOrDefault();
        }
Example #5
0
        public ReflectionMethodBase(
            MethodBase methodBase,
            IReflectionFactory factory,
            ParameterInfo[] parameters        = null,
            IReflectionTypeInfo declaringType = null)
        {
            if (methodBase == null)
            {
                throw new ArgumentNullException(nameof(methodBase));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            this.MethodBase        = methodBase;
            this.ReflectionFactory = factory;
            this.DeclaringType     = declaringType;

            if (parameters != null)
            {
                this.parameters = new List <IReflectionParameter>();

                foreach (var param in parameters)
                {
                    this.parameters.Add(this.ReflectionFactory.CreateParameter(param));
                }
            }
        }
Example #6
0
        protected override async Task <RunSummary> RunTestClassAsync(ITestClass testClass,
                                                                     IReflectionTypeInfo @class,
                                                                     IEnumerable <ObservationTestCase> testCases)
        {
            var timer         = new ExecutionTimer();
            var specification = Activator.CreateInstance(testClass.Class.ToRuntimeType()) as Specification;

            if (specification == null)
            {
                Aggregator.Add(new InvalidOperationException(String.Format("Test class {0} cannot be static, and must derive from Specification.", testClass.Class.Name)));
                return(FailedSummary);
            }

            Aggregator.Run(specification.OnStart);
            if (Aggregator.HasExceptions)
            {
                return(FailedSummary);
            }

            var result = await new ObservationTestClassRunner(specification, testClass, @class, testCases, diagnosticMessageSink, MessageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), CancellationTokenSource).RunAsync();

            Aggregator.Run(specification.OnFinish);

            var disposable = specification as IDisposable;

            if (disposable != null)
            {
                timer.Aggregate(disposable.Dispose);
            }

            return(result);
        }
        protected override Task <RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable <IXunitTestCase> testCases)
        {
            foreach (var fixtureType in @class.Type.GetTypeInfo().ImplementedInterfaces
                     .Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IAssemblyFixture <>))
                     .Select(i => i.GetTypeInfo().GenericTypeArguments.Single())
                     // First pass at filtering out before locking
                     .Where(i => !assemblyFixtureMappings.ContainsKey(i)))
            {
                // ConcurrentDictionary's GetOrAdd does not lock around the value factory call, so we need
                // to do it ourselves.
                lock (assemblyFixtureMappings)
                    if (!assemblyFixtureMappings.ContainsKey(fixtureType))
                    {
                        Aggregator.Run(() => assemblyFixtureMappings.Add(fixtureType, CreateAssemblyFixtureInstance(fixtureType)));
                    }
            }

            // Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
            // so instead we'll just let collection fixtures override assembly fixtures.
            var combinedFixtures = new Dictionary <Type, object>(assemblyFixtureMappings);

            foreach (var kvp in CollectionFixtureMappings)
            {
                combinedFixtures[kvp.Key] = kvp.Value;
            }

            // We've done everything we need, so let the built-in types do the rest of the heavy lifting
            return(new XunitTestClassRunner(testClass, @class, testCases, diagnosticMessageSink, MessageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), CancellationTokenSource, combinedFixtures).RunAsync());
        }
        protected override Task <RunSummary> RunTestClassAsync(
            ITestClass testClass,
            IReflectionTypeInfo @class,
            IEnumerable <IXunitTestCase> testCases)
        {
            // Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
            // so instead we'll just let collection fixtures override assembly fixtures.
            var combinedFixtures = new Dictionary <Type, object>(AssemblyFixtureMappings);

            foreach (KeyValuePair <Type, object> kvp in CollectionFixtureMappings)
            {
                combinedFixtures[kvp.Key] = kvp.Value;
            }

            return
                (new XunitTestClassRunner(
                     testClass,
                     @class,
                     testCases,
                     DiagnosticMessageSink,
                     MessageBus,
                     TestCaseOrderer,
                     new ExceptionAggregator(Aggregator),
                     CancellationTokenSource,
                     combinedFixtures)
                 .RunAsync());
        }
 public virtual IReflectionField CreateField(
     FieldInfo info,
     IReflectionTypeInfo declaringType = null)
 {
     // return new ReflectionProperty(info, declaringType);
     return(new ReflectionField(info, declaringType));
 }
Example #10
0
 public TestMethodRunner(ITestMethod testMethod, IReflectionTypeInfo @class, IReflectionMethodInfo method, IEnumerable <IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, object[] constructorArguments, IReadOnlyDictionary <Type, object> assemblyFixtureMappings)
     : base(testMethod, @class, method, testCases, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource, constructorArguments)
 {
     _hasMethodFixtures       = constructorArguments.OfType <IMethodFixture>().Any();
     _assemblyFixtureMappings = assemblyFixtureMappings;
     _diagnosticMessageSink   = diagnosticMessageSink;
     _constructorArguments    = constructorArguments;
 }
 /// <inheritdoc />
 protected override Task <RunSummary> RunTestClassAsync(ITestClass testClass,
                                                        IReflectionTypeInfo @class, IEnumerable <IXunitTestCase> testCases) =>
 _hostMap.TryGetValue(testClass, out var host) && host != null
         ? new DependencyInjectionTestClassRunner(host.Services, testClass, @class, testCases,
                                                  _diagnosticMessageSink, MessageBus, TestCaseOrderer,
                                                  new ExceptionAggregator(Aggregator), CancellationTokenSource, CollectionFixtureMappings)
 .RunAsync()
         : base.RunTestClassAsync(testClass, @class, testCases);
 public TestMethodRunner(ITestMethod testMethod, IReflectionTypeInfo @class, IReflectionMethodInfo method, IEnumerable <IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, object[] constructorArguments, Dictionary <Type, object> classFixtureMappings, Type testNotificationType)
     : base(testMethod, @class, method, testCases, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource, constructorArguments)
 {
     _diagnosticMessageSink = diagnosticMessageSink;
     _constructorArguments  = constructorArguments;
     _classFixtureMappings  = classFixtureMappings;
     _testNotificationType  = testNotificationType;
 }
Example #13
0
 public TestMethodRunner(ITestMethod testMethod, IReflectionTypeInfo @class, IReflectionMethodInfo method, IEnumerable <IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, object[] constructorArguments, IReadOnlyDictionary <Type, object> assemblyFixtureMappings, XunitTestEnvironment testEnvironment)
     : base(testMethod, @class, method, testCases, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource, constructorArguments)
 {
     _assemblyFixtureMappings = assemblyFixtureMappings;
     _testEnvironment         = testEnvironment;
     _diagnosticMessageSink   = diagnosticMessageSink;
     _constructorArguments    = constructorArguments;
     _stopwatch = new Stopwatch();
 }
Example #14
0
 public ReflectionConstructor(
     ConstructorInfo constructorInfo,
     IReflectionFactory factory,
     ParameterInfo[] parameters        = null,
     IReflectionTypeInfo declaringType = null)
     : base(constructorInfo, factory, parameters, declaringType)
 {
     this.ConstructorInfo = constructorInfo;
 }
Example #15
0
 public ReflectionMethod(
     MethodInfo info,
     IReflectionFactory factory,
     ParameterInfo[] parameters        = null,
     IReflectionTypeInfo declaringType = null)
     : base(info, factory, parameters, declaringType)
 {
     this.MethodInfo = info;
 }
 public XunitTestClassRunnerWithAssemblyFixture(
     List <AssemblyFixtureAttribute> assemblyFixtureAttributes,
     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.assemblyFixtureAttributes = assemblyFixtureAttributes;
 }
Example #17
0
        public ReflectionField(FieldInfo info, IReflectionTypeInfo delcaringType = null)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            this.FieldInfo     = info;
            this.DeclaringType = delcaringType;
        }
Example #18
0
 public AutofacTestClassRunner(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable <IXunitTestCase> testCases,
                               IMessageSink diagnosticMessageSink,
                               IMessageBus messageBus,
                               ITestCaseOrderer testCaseOrderer, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, IDictionary <Type, object> collectionFixtureMappings,
                               ILifetimeScope lifetimeScope
                               ) : base(testClass, @class, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings)
 {
     _collectionFixtureMappings = collectionFixtureMappings;
     _lifetimeScope             = lifetimeScope;
 }
Example #19
0
        protected override Task <RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo reflectionTypeInfo, IEnumerable <AutofacTestCase> testCases)
        {
            var exceptionAggregator = new ExceptionAggregator(Aggregator);

            var autofacTestClassRunner = new AutofacTestClassRunner(_container, testClass, reflectionTypeInfo, testCases,
                                                                    _diagnosticMessageSink, MessageBus, TestCaseOrderer, exceptionAggregator,
                                                                    CancellationTokenSource);

            return(autofacTestClassRunner.RunAsync());
        }
		protected override Task<RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable<IXunitTestCase> testCases)
		{
			var combinedFixtures = new Dictionary<Type, object>(assemblyFixtureMappings);
			foreach (var kvp in CollectionFixtureMappings)
				combinedFixtures[kvp.Key] = kvp.Value;

			// We've done everything we need, so let the built-in types do the rest of the heavy lifting

			return new XunitTestClassRunner(testClass, @class, testCases, diagnosticMessageSink, MessageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), CancellationTokenSource, combinedFixtures).RunAsync();
		}
Example #21
0
 public virtual IReflectionConstructor CreateConstructor(
     ConstructorInfo info,
     ParameterInfo[] parameters        = null,
     IReflectionTypeInfo declaringType = null)
 {
     return(new ReflectionConstructor(
                info,
                this,
                parameters,
                declaringType));
 }
Example #22
0
 public virtual IReflectionMethod CreateMethod(
     MethodInfo info,
     ParameterInfo[] parameters        = null,
     IReflectionTypeInfo declaringType = null)
 {
     return(new ReflectionMethod(
                info,
                this,
                parameters,
                declaringType));
 }
Example #23
0
        protected override Task <RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable <ITestCase> testCases)
        {
            if (cancelInRunTestClassAsync)
            {
                CancellationTokenSource.Cancel();
            }

            RunTestClassAsync_AggregatorResult = Aggregator.ToException();
            ClassesRun.Add(Tuple.Create(@class, testCases));
            return(Task.FromResult(result));
        }
 TestableXunitTestClassRunner(ITestClass testClass,
                              IReflectionTypeInfo @class,
                              IEnumerable <IXunitTestCase> testCases,
                              IMessageBus messageBus,
                              ITestCaseOrderer testCaseOrderer,
                              ExceptionAggregator aggregator,
                              CancellationTokenSource cancellationTokenSource,
                              IDictionary <Type, object> collectionFixtureMappings)
     : base(testClass, @class, testCases, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings)
 {
 }
Example #25
0
 // ReSharper disable once TooManyDependencies
 public TestMethodRunner(ITestMethod testMethod, IReflectionTypeInfo @class, IReflectionMethodInfo method,
                         IEnumerable <IXunitTestCase> testCases, IMessageSink diagnosticMessageSink,
                         IMessageBus messageBus, ExceptionAggregator aggregator,
                         CancellationTokenSource cancellationTokenSource, object[] constructorArguments)
     : base(testMethod, @class, method, testCases, diagnosticMessageSink, messageBus, aggregator,
            cancellationTokenSource,
            constructorArguments)
 {
     _diagnosticMessageSink = diagnosticMessageSink;
     _constructorArguments  = constructorArguments;
 }
Example #26
0
 public ObservationTestMethodRunner(SpecificationBase specification,
                                    ITestMethod testMethod,
                                    IReflectionTypeInfo @class,
                                    IReflectionMethodInfo method,
                                    IEnumerable <ObservationTestCase> testCases,
                                    IMessageBus messageBus,
                                    ExceptionAggregator aggregator,
                                    CancellationTokenSource cancellationTokenSource)
     : base(testMethod, @class, method, testCases, messageBus, aggregator, cancellationTokenSource)
 {
     this.specification = specification;
 }
 protected override Task <RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable <IXunitTestCase> testCases) =>
 new XunitTestClassRunnerWithInjectionSupport(
     testClass,
     @class,
     testCases,
     this.diagnosticMessageSink,
     this.MessageBus,
     this.TestCaseOrderer,
     new ExceptionAggregator(this.Aggregator),
     this.CancellationTokenSource,
     this.CollectionFixtureMappings)
 .RunAsync();
Example #28
0
 public XunitTestClassRunner(IMessageBus messageBus,
                             ITestCollection testCollection,
                             IReflectionTypeInfo testClass,
                             IEnumerable <IXunitTestCase> testCases,
                             ITestCaseOrderer testCaseOrderer,
                             ExceptionAggregator aggregator,
                             CancellationTokenSource cancellationTokenSource,
                             IDictionary <Type, object> collectionFixtureMappings)
     : base(messageBus, testCollection, testClass, testCases, testCaseOrderer, aggregator, cancellationTokenSource)
 {
     this.collectionFixtureMappings = collectionFixtureMappings;
 }
Example #29
0
 public PerformanceTestClassRunner(
     ITestClass testClass,
     IReflectionTypeInfo @class,
     IEnumerable <IXunitTestCase> testCases,
     IMessageSink diagnosticMessageSink,
     IMessageBus messageBus,
     ITestCaseOrderer testCaseOrderer,
     ExceptionAggregator aggregator,
     CancellationTokenSource cancellationTokenSource,
     IDictionary <Type, object> collectionFixtureMappings,
     TestResourceSnapshotWriter testResourceSnapshotWriter,
     in bool resourceSnapshotEnabled) : base(testClass, @class, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings)
 /// <inheritdoc/>
 protected override Task <RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable <IXunitTestCase> testCases)
 => new MettleTestClassRunner(
     testClass,
     @class,
     testCases,
     DiagnosticMessageSink,
     MessageBus,
     TestCaseOrderer,
     new ExceptionAggregator(Aggregator),
     CancellationTokenSource,
     CollectionFixtureMappings,
     this.serviceProvider).RunAsync();
 TestableXunitTestClassRunner(ITestClass testClass,
                              IReflectionTypeInfo @class,
                              IEnumerable <IXunitTestCase> testCases,
                              List <IMessageSinkMessage> diagnosticMessages,
                              IMessageBus messageBus,
                              ITestCaseOrderer testCaseOrderer,
                              ExceptionAggregator aggregator,
                              CancellationTokenSource cancellationTokenSource,
                              IDictionary <Type, object> collectionFixtureMappings)
     : base(testClass, @class, testCases, SpyMessageSink.Create(messages: diagnosticMessages), messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings)
 {
     DiagnosticMessages = diagnosticMessages;
 }
		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;
		}
		protected override Task<RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable<IXunitTestCase> testCases)
		{
			foreach (var fixtureType in @class.Type.GetTypeInfo().ImplementedInterfaces
					.Where(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IAssemblyFixture<>))
					.Select(i => i.GetTypeInfo().GenericTypeArguments.Single())
					// First pass at filtering out before locking
					.Where(i => !assemblyFixtureMappings.ContainsKey(i)))
			{
				// ConcurrentDictionary's GetOrAdd does not lock around the value factory call, so we need
				// to do it ourselves.
				lock (assemblyFixtureMappings)
					if (!assemblyFixtureMappings.ContainsKey(fixtureType))
						Aggregator.Run(() => assemblyFixtureMappings.Add(fixtureType, Activator.CreateInstance(fixtureType)));
			}

			// Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
			// so instead we'll just let collection fixtures override assembly fixtures.
			var combinedFixtures = new Dictionary<Type, object>(assemblyFixtureMappings);
			foreach (var kvp in CollectionFixtureMappings)
				combinedFixtures[kvp.Key] = kvp.Value;

			// We've done everything we need, so let the built-in types do the rest of the heavy lifting
			return new XunitTestClassRunner(testClass, @class, testCases, diagnosticMessageSink, MessageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), CancellationTokenSource, combinedFixtures).RunAsync();
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Xunit.Sdk.XunitTestMethodRunner"/> class.
 /// </summary>
 /// <param name="testMethod">The test method to be run.</param><param name="class">The test class that contains the test method.</param><param name="method">The test method that contains the tests to be run.</param><param name="testCases">The test cases to be run.</param><param name="diagnosticMessageSink">The message sink used to send diagnostic messages to.</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="constructorArguments">The constructor arguments for the test class.</param>
 public BrowserTestMethodRunner(ITestMethod testMethod, IReflectionTypeInfo @class, IReflectionMethodInfo method, IEnumerable<IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, object[] constructorArguments)
     : base(testMethod, @class, method, testCases, diagnosticMessageSink, messageBus, aggregator, cancellationTokenSource, constructorArguments)
 {
     DiagnosticMessageSink = diagnosticMessageSink;
     ConstructorArguments = constructorArguments;
 }
		protected override Task<RunSummary> RunTestClassAsync (ITestClass testClass, IReflectionTypeInfo @class, IEnumerable<IXunitTestCase> testCases)
		{
            return new VsixTestClassRunner(vs, testClass, @class, testCases, diagnosticMessageSink, MessageBus, TestCaseOrderer, 
				Aggregator, CancellationTokenSource, CollectionFixtureMappings).RunAsync();
		}
Example #36
0
 public ClassRunner(ITestClass testClass, IReflectionTypeInfo typeInfo, IEnumerable<IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ITestCaseOrderer testCaseOrderer, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, IDictionary<Type, object> collectionFixtureMappings, IReadOnlyDictionary<Type, object> assemblyFixtureMappings)
     : base(testClass, typeInfo, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings) {
     _assemblyFixtureMappings = assemblyFixtureMappings;
 }
 /// <inheritdoc/>
 protected override Task<RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable<IXunitTestCase> testCases)
     => new BrowserTestClassRunner(testClass, @class, testCases, DiagnosticMessageSink, MessageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), CancellationTokenSource, CollectionFixtureMappings).RunAsync();
        private static async Task RunTestClassAsync(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
                    await RunTestMethodAsync(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();
                }
            }
        }