internal static Type GetTestCollectionFactoryType(IAttributeInfo collectionBehavior)
        {
            if (collectionBehavior == null)
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            var ctorArgs = collectionBehavior.GetConstructorArguments().ToList();

            if (ctorArgs.Count == 0)
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            if (ctorArgs.Count == 1 && (CollectionBehavior)ctorArgs[0] == CollectionBehavior.CollectionPerAssembly)
            {
                return(typeof(CollectionPerAssemblyTestCollectionFactory));
            }

            var result = Reflector.GetType((string)ctorArgs[1], (string)ctorArgs[0]);

            if (!typeof(IXunitTestCollectionFactory).IsAssignableFrom(result) || result.GetConstructor(new[] { typeof(IAssemblyInfo) }) == null)
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            return(result);
        }
        static ITestCaseOrderer GetTestCaseOrderer(IAttributeInfo ordererAttribute)
        {
            var args        = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var ordererType = Reflector.GetType(args[1], args[0]);

            return((ITestCaseOrderer)Activator.CreateInstance(ordererType));
        }
        /// <summary>
        /// Core implementation to discover unit tests in a given test class.
        /// </summary>
        /// <param name="type">The test class.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to attempt to include source information.</param>
        /// <param name="messageBus">The message sink to send discovery messages to.</param>
        /// <returns>Returns <c>true</c> if discovery should continue; <c>false</c> otherwise.</returns>
        protected virtual bool FindImpl(ITypeInfo type, bool includeSourceInformation, IMessageBus messageBus)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            var    testCollection   = TestCollectionFactory.Get(type);

            try
            {
                if (!String.IsNullOrEmpty(assemblyInfo.AssemblyPath))
                {
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(assemblyInfo.AssemblyPath));
                }

                foreach (var method in type.GetMethods(includePrivateMethods: true))
                {
                    var factAttribute = method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();
                    if (factAttribute != null)
                    {
                        var discovererAttribute = factAttribute.GetCustomAttributes(typeof(TestCaseDiscovererAttribute)).FirstOrDefault();
                        if (discovererAttribute != null)
                        {
                            var args           = discovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                            var discovererType = Reflector.GetType(args[1], args[0]);
                            if (discovererType != null)
                            {
                                var discoverer = GetDiscoverer(discovererType);

                                if (discoverer != null)
                                {
                                    foreach (var testCase in discoverer.Discover(testCollection, assemblyInfo, type, method, factAttribute))
                                    {
                                        if (!messageBus.QueueMessage(new TestCaseDiscoveryMessage(UpdateTestCaseWithSourceInfo(testCase, includeSourceInformation))))
                                        {
                                            return(false);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                messageAggregator.Add(new EnvironmentalWarning {
                                    Message = String.Format("Could not create discoverer type '{0}, {1}'", args[0], args[1])
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                messageAggregator.Add(new EnvironmentalWarning {
                    Message = String.Format("Exception during discovery:{0}{1}", Environment.NewLine, ex)
                });
            }
            finally
            {
                Directory.SetCurrentDirectory(currentDirectory);
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestFrameworkProxy"/> class.
        /// </summary>
        /// <param name="testAssemblyObject">The test assembly (expected to implement <see cref="IAssemblyInfo"/>).</param>
        /// <param name="sourceInformationProviderObject">The source information provider (expected to implement <see cref="ISourceInformationProvider"/>).</param>
        public TestFrameworkProxy(object testAssemblyObject, object sourceInformationProviderObject)
        {
            var testAssembly = (IAssemblyInfo)testAssemblyObject;
            var sourceInformationProvider = (ISourceInformationProvider)sourceInformationProviderObject;

            Type testFrameworkType = typeof(XunitTestFramework);

            try
            {
                var attr = testAssembly.GetCustomAttributes(typeof(TestFrameworkAttribute)).FirstOrDefault();
                if (attr != null)
                {
                    var ctorArgs = attr.GetConstructorArguments().Cast <string>().ToArray();
                    testFrameworkType = Reflector.GetType(ctorArgs[1], ctorArgs[0]);
                }
            }
            catch
            {
                // TODO: Log environmental error
            }

            try
            {
                testFramework = (ITestFramework)Activator.CreateInstance(testFrameworkType);
            }
            catch
            {
                // TODO: Log environmental error
                testFramework = new XunitTestFramework();
            }

            SourceInformationProvider = sourceInformationProvider;
        }
        static Type GetTestCollectionFactoryType(IAttributeInfo collectionBehaviorAttribute)
        {
            if (collectionBehaviorAttribute == null)
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            var ctorArgs = collectionBehaviorAttribute.GetConstructorArguments().ToList();

            if (ctorArgs.Count == 0)
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            if (ctorArgs.Count == 1)
            {
                if ((CollectionBehavior)ctorArgs[0] == CollectionBehavior.CollectionPerAssembly)
                {
                    return(typeof(CollectionPerAssemblyTestCollectionFactory));
                }

                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            var result = Reflector.GetType((string)ctorArgs[1], (string)ctorArgs[0]);

            if (result == null || !IsCompatibleTestCollectionFactory(result))
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            return(result);
        }
Esempio n. 6
0
        static ITestCaseOrderer GetXunitTestCaseOrderer(IAttributeInfo ordererAttribute)
        {
            var args        = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var ordererType = Reflector.GetType(args[1], args[0]);

            return(ExtensibilityPointFactory.GetTestCaseOrderer(ordererType));
        }
Esempio n. 7
0
        /// <inheritdoc/>
        protected XunitTestClass(SerializationInfo info, StreamingContext context)
        {
            TestCollection = info.GetValue <ITestCollection>("TestCollection");

            var assemblyName = info.GetString("ClassAssemblyName");
            var typeName     = info.GetString("ClassTypeName");

            Class = Reflector.Wrap(Reflector.GetType(assemblyName, typeName));
        }
Esempio n. 8
0
        /// <inheritdoc/>
        public void SetData(XunitSerializationInfo info)
        {
            TestCollection = info.GetValue <ITestCollection>("TestCollection");

            var assemblyName = info.GetString("ClassAssemblyName");
            var typeName     = info.GetString("ClassTypeName");

            Class = Reflector.Wrap(Reflector.GetType(assemblyName, typeName));
        }
Esempio n. 9
0
        /// <summary>
        /// Gets the <see cref="Type"/> of the class under test.
        /// </summary>
        /// <returns>The type under test, if possible; null, if not available.</returns>
        protected Type GetRuntimeClass()
        {
            var reflectionTypeInfo = Class as IReflectionTypeInfo;

            if (reflectionTypeInfo != null)
            {
                return(reflectionTypeInfo.Type);
            }

            return(Reflector.GetType(Assembly.Name, Class.Name));
        }
Esempio n. 10
0
        Type GetRuntimeClass()
        {
            var testClass          = TestCase.TestMethod.TestClass;
            var reflectionTypeInfo = testClass.Class as IReflectionTypeInfo;

            if (reflectionTypeInfo != null)
            {
                return(reflectionTypeInfo.Type);
            }

            return(Reflector.GetType(testClass.TestCollection.TestAssembly.Assembly.Name, testClass.Class.Name));
        }
Esempio n. 11
0
        /// <inheritdoc/>
        protected XunitTestCollection(SerializationInfo info, StreamingContext context)
        {
            DisplayName = info.GetString("DisplayName");

            var assemblyName = info.GetString("DeclarationAssemblyName");
            var typeName     = info.GetString("DeclarationTypeName");

            if (!String.IsNullOrWhiteSpace(assemblyName) && String.IsNullOrWhiteSpace(typeName))
            {
                CollectionDefinition = Reflector.Wrap(Reflector.GetType(assemblyName, typeName));
            }
        }
Esempio n. 12
0
        /// <inheritdoc/>
        public IEnumerable <IXunitTestCase> Discover(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo testClass, IMethodInfo testMethod, IAttributeInfo factAttribute)
        {
            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument <string>("Skip") != null)
            {
                return new[] { new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute) }
            }
            ;

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    var results = new List <XunitTestCase>();

                    var dataAttributes = testMethod.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);

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (object[] dataRow in discoverer.GetData(dataAttribute, testMethod))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute, dataRow);
                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    // REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
                    if (results.Count == 0)
                    {
                        results.Add(new LambdaTestCase(testCollection, assembly, testClass, testMethod, factAttribute, () => { throw new InvalidOperationException("No data found for " + testClass.Name + "." + testMethod.Name); }));
                    }

                    return(results);
                }
            }
            catch
            {
                return(new XunitTestCase[] { new XunitTheoryTestCase(testCollection, assembly, testClass, testMethod, factAttribute) });
            }
        }
    }
        /// <summary>
        /// Gets a test case orderer, as specified in a reflected <see cref="TestCaseOrdererAttribute"/>.
        /// </summary>
        /// <param name="testCaseOrdererAttribute">The test case orderer attribute.</param>
        /// <returns>The test case orderer, if the type is loadable; <c>null</c>, otherwise.</returns>
        public static ITestCaseOrderer GetTestCaseOrderer(IAttributeInfo testCaseOrdererAttribute)
        {
            var args        = testCaseOrdererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var ordererType = Reflector.GetType(args[1], args[0]);

            if (ordererType == null)
            {
                return(null);
            }

            return(GetTestCaseOrderer(ordererType));
        }
        /// <summary>
        /// Gets a test framework discoverer, as specified in a reflected <see cref="TestFrameworkDiscovererAttribute"/>.
        /// </summary>
        public static ITestFrameworkTypeDiscoverer GetTestFrameworkTypeDiscoverer(IAttributeInfo testFrameworkAttribute)
        {
            var args = testFrameworkAttribute.GetConstructorArguments().Cast <string>().ToArray();
            var testFrameworkDiscovererType = Reflector.GetType(args[1], args[0]);

            if (testFrameworkDiscovererType == null)
            {
                return(null);
            }

            return(GetTestFrameworkTypeDiscoverer(testFrameworkDiscovererType));
        }
        /// <summary>
        /// Gets a data discoverer, as specified in a reflected <see cref="DataDiscovererAttribute"/>.
        /// </summary>
        /// <param name="dataDiscovererAttribute">The data discoverer attribute</param>
        /// <returns>The data discoverer, if the type is loadable; <c>null</c>, otherwise.</returns>
        public static IDataDiscoverer GetDataDiscoverer(IAttributeInfo dataDiscovererAttribute)
        {
            var args           = dataDiscovererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var discovererType = Reflector.GetType(args[1], args[0]);

            if (discovererType == null)
            {
                return(null);
            }

            return(GetDataDiscoverer(discovererType));
        }
Esempio n. 16
0
        /// <inheritdoc/>
        public void SetData(XunitSerializationInfo info)
        {
            DisplayName  = info.GetString("DisplayName");
            TestAssembly = info.GetValue <ITestAssembly>("TestAssembly");
            UniqueID     = Guid.Parse(info.GetString("UniqueID"));

            var assemblyName = info.GetString("DeclarationAssemblyName");
            var typeName     = info.GetString("DeclarationTypeName");

            if (!String.IsNullOrWhiteSpace(assemblyName) && String.IsNullOrWhiteSpace(typeName))
            {
                CollectionDefinition = Reflector.Wrap(Reflector.GetType(assemblyName, typeName));
            }
        }
Esempio n. 17
0
        /// <inheritdoc/>
        protected TestCollection(SerializationInfo info, StreamingContext context)
        {
            DisplayName  = info.GetString("DisplayName");
            TestAssembly = info.GetValue <ITestAssembly>("TestAssembly");
            UniqueID     = Guid.Parse(info.GetString("UniqueID"));

            var assemblyName = info.GetString("DeclarationAssemblyName");
            var typeName     = info.GetString("DeclarationTypeName");

            if (!String.IsNullOrWhiteSpace(assemblyName) && String.IsNullOrWhiteSpace(typeName))
            {
                CollectionDefinition = Reflector.Wrap(Reflector.GetType(assemblyName, typeName));
            }
        }
Esempio n. 18
0
        /// <inheritdoc/>
        protected XunitTestCase(SerializationInfo info, StreamingContext context)
        {
            string assemblyName = info.GetString("AssemblyName");
            string typeName     = info.GetString("TypeName");
            string methodName   = info.GetString("MethodName");

            object[] arguments      = (object[])info.GetValue("Arguments", typeof(object[]));
            var      testCollection = (ITestCollection)info.GetValue("TestCollection", typeof(ITestCollection));

            var type          = Reflector.GetType(assemblyName, typeName);
            var typeInfo      = Reflector.Wrap(type);
            var methodInfo    = Reflector.Wrap(type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static));
            var factAttribute = methodInfo.GetCustomAttributes(typeof(FactAttribute)).Single();

            Initialize(testCollection, Reflector.Wrap(type.Assembly), typeInfo, methodInfo, factAttribute, arguments);
        }
Esempio n. 19
0
        /// <summary>
        /// Finds the tests on a test method.
        /// </summary>
        /// <param name="testMethod">The test method.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to indicate that source information should be included.</param>
        /// <param name="messageBus">The message bus to report discovery messages to.</param>
        /// <param name="discoveryOptions">The options used by the test framework during discovery.</param>
        /// <returns>Return <c>true</c> to continue test discovery, <c>false</c>, otherwise.</returns>
        protected virtual bool FindTestsForMethod(ITestMethod testMethod, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkOptions discoveryOptions)
        {
            var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();

            if (factAttribute == null)
            {
                return(true);
            }

            var testCaseDiscovererAttribute = factAttribute.GetCustomAttributes(typeof(XunitTestCaseDiscovererAttribute)).FirstOrDefault();

            if (testCaseDiscovererAttribute == null)
            {
                return(true);
            }

            var args           = testCaseDiscovererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var discovererType = Reflector.GetType(args[1], args[0]);

            if (discovererType == null)
            {
                return(true);
            }

            var discoverer = GetDiscoverer(discovererType);

            if (discoverer == null)
            {
                return(true);
            }

            var methodDisplayString = discoveryOptions.GetValue <string>(TestOptionsNames.Discovery.MethodDisplay, null);
            var methodDisplay       = methodDisplayString == null ? TestMethodDisplay.ClassAndMethod : (TestMethodDisplay)Enum.Parse(typeof(TestMethodDisplay), methodDisplayString);

            foreach (var testCase in discoverer.Discover(methodDisplay, testMethod, factAttribute))
            {
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <inheritdoc/>
        protected override async Task AfterTestCaseStartingAsync()
        {
            await base.AfterTestCaseStartingAsync();

            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);
                        var test = new XunitTest(TestCase, theoryDisplayName);

                        testRunners.Add(new XunitTestRunner(test, MessageBus, TestClass, ConstructorArguments, methodToRun, convertedDataRow, SkipReason, BeforeAfterAttributes, Aggregator, CancellationTokenSource));
                    }
                }
            }
            catch (Exception ex)
            {
                // Stash the exception so we can surface it during RunTestAsync
                dataDiscoveryException = ex;
            }
        }
Esempio n. 21
0
        void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
        {
            string displayNameBase = factAttribute.GetNamedArgument <string>("DisplayName") ?? type.Name + "." + method.Name;

            ITypeInfo[] resolvedTypes = null;

            if (arguments != null && method.IsGenericMethodDefinition)
            {
                resolvedTypes = ResolveGenericTypes(method, arguments);
                method        = method.MakeGenericMethod(resolvedTypes);
            }

            Assembly       = assembly;
            Class          = type;
            Method         = method;
            Arguments      = arguments;
            DisplayName    = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
            SkipReason     = factAttribute.GetNamedArgument <string>("Skip");
            Traits         = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);
            TestCollection = testCollection;

            foreach (var traitAttribute in Method.GetCustomAttributes(typeof(ITraitAttribute))
                     .Concat(Class.GetCustomAttributes(typeof(ITraitAttribute))))
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).First();
                var args           = discovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                var discovererType = Reflector.GetType(args[1], args[0]);

                if (discovererType != null)
                {
                    var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(discovererType);

                    foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                    {
                        Traits.Add(keyValuePair.Key, keyValuePair.Value);
                    }
                }
            }

            uniqueID = new Lazy <string>(GetUniqueID, true);
        }
Esempio n. 22
0
        /// <summary>
        /// Finds the tests on a test method.
        /// </summary>
        /// <param name="testMethod">The test method.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to indicate that source information should be included.</param>
        /// <param name="messageBus">The message bus to report discovery messages to.</param>
        /// <returns>Return <c>true</c> to continue test discovery, <c>false</c>, otherwise.</returns>
        protected virtual bool FindTestsForMethod(ITestMethod testMethod, bool includeSourceInformation, IMessageBus messageBus)
        {
            var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();

            if (factAttribute == null)
            {
                return(true);
            }

            var testCaseDiscovererAttribute = factAttribute.GetCustomAttributes(typeof(XunitTestCaseDiscovererAttribute)).FirstOrDefault();

            if (testCaseDiscovererAttribute == null)
            {
                return(true);
            }

            var args           = testCaseDiscovererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var discovererType = Reflector.GetType(args[1], args[0]);

            if (discovererType == null)
            {
                return(true);
            }

            var discoverer = GetDiscoverer(discovererType);

            if (discoverer == null)
            {
                return(true);
            }

            foreach (var testCase in discoverer.Discover(testMethod, factAttribute))
            {
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 23
0
        static IEnumerable <IAttributeInfo> GetCustomAttributes(MethodInfo method, string assemblyQualifiedAttributeTypeName)
        {
            var attributeType = Reflector.GetType(assemblyQualifiedAttributeTypeName);

            return(GetCustomAttributes(method, attributeType, ReflectionAttributeInfo.GetAttributeUsage(attributeType)));
        }
Esempio n. 24
0
        internal static IEnumerable <IAttributeInfo> GetCustomAttributes(Type type, string assemblyQualifiedAttributeTypeName)
        {
            Type attributeType = Reflector.GetType(assemblyQualifiedAttributeTypeName);

            return(GetCustomAttributes(type, attributeType, GetAttributeUsage(attributeType)));
        }
        /// <inheritdoc/>
        public Type GetTestFrameworkType(IAttributeInfo attribute)
        {
            var args = attribute.GetConstructorArguments().Cast <string>().ToArray();

            return(Reflector.GetType(args[1], args[0]));
        }
Esempio n. 26
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);
        }
Esempio n. 27
0
        /// <inheritdoc />
        protected override async Task RunTestsOnMethodAsync(IMessageBus messageBus,
                                                            Type classUnderTest,
                                                            object[] constructorArguments,
                                                            MethodInfo methodUnderTest,
                                                            List <BeforeAfterTestAttribute> beforeAfterAttributes,
                                                            ExceptionAggregator aggregator,
                                                            CancellationTokenSource cancellationTokenSource)
        {
            var executionTime = 0M;

            try
            {
                var testMethod = Reflector.Wrap(methodUnderTest);

                var dataAttributes = testMethod.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 (object[] dataRow in discoverer.GetData(dataAttribute, testMethod))
                    {
                        var         methodToRun   = methodUnderTest;
                        ITypeInfo[] resolvedTypes = null;

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

                        executionTime +=
                            await RunTestWithArgumentsAsync(messageBus,
                                                            classUnderTest,
                                                            constructorArguments,
                                                            methodToRun,
                                                            dataRow,
                                                            GetDisplayNameWithArguments(DisplayName, dataRow, resolvedTypes),
                                                            beforeAfterAttributes,
                                                            aggregator,
                                                            cancellationTokenSource);

                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (!messageBus.QueueMessage(new TestStarting(this, DisplayName)))
                {
                    cancellationTokenSource.Cancel();
                }
                else
                {
                    if (!messageBus.QueueMessage(new TestFailed(this, DisplayName, executionTime, null, ex.Unwrap())))
                    {
                        cancellationTokenSource.Cancel();
                    }
                }

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