/// <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 internal virtual bool FindTestsForMethod(ITestMethod testMethod, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkDiscoveryOptions discoveryOptions)
        {
            var factAttributes = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).CastOrToList();

            if (factAttributes.Count > 1)
            {
                var message  = $"Test method '{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}' has multiple [Fact]-derived attributes";
                var testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod, message);
                return(ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus));
            }

            var factAttribute = factAttributes.FirstOrDefault();

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

            var factAttributeType = (factAttribute as IReflectionAttributeInfo)?.Attribute.GetType();

            Type discovererType = null;

            if (factAttributeType == null || !DiscovererTypeCache.TryGetValue(factAttributeType, out discovererType))
            {
                var testCaseDiscovererAttribute = factAttribute.GetCustomAttributes(typeof(XunitTestCaseDiscovererAttribute)).FirstOrDefault();
                if (testCaseDiscovererAttribute != null)
                {
                    var args = testCaseDiscovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                    discovererType = SerializationHelper.GetType(args[1], args[0]);
                }

                if (factAttributeType != null)
                {
                    DiscovererTypeCache[factAttributeType] = discovererType;
                }
            }
            if (discovererType == null)
            {
                return(true);
            }

            var discoverer = GetDiscoverer(discovererType);

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

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

            return(true);
        }
Пример #2
0
        /// <inheritdoc/>
        public void Deserialize(IXunitSerializationInfo info)
        {
            TestCollection = info.GetValue <ITestCollection>("TestCollection");

            var assemblyName = info.GetValue <string>("ClassAssemblyName");
            var typeName     = info.GetValue <string>("ClassTypeName");

            Class = Reflector.Wrap(SerializationHelper.GetType(assemblyName, typeName));
        }
Пример #3
0
        /// <summary>
        /// Gets a data discoverer, as specified in a reflected <see cref="DataDiscovererAttribute"/>.
        /// </summary>
        /// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
        /// <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(IMessageSink diagnosticMessageSink, IAttributeInfo dataDiscovererAttribute)
        {
            var args           = dataDiscovererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var discovererType = SerializationHelper.GetType(args[1], args[0]);

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

            return(GetDataDiscoverer(diagnosticMessageSink, discovererType));
        }
Пример #4
0
        /// <summary>
        /// Gets a test framework discoverer, as specified in a reflected <see cref="TestFrameworkDiscovererAttribute"/>.
        /// </summary>
        /// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
        /// <param name="testFrameworkDiscovererAttribute">The test framework discoverer attribute</param>
        public static ITestFrameworkTypeDiscoverer GetTestFrameworkTypeDiscoverer(IMessageSink diagnosticMessageSink, IAttributeInfo testFrameworkDiscovererAttribute)
        {
            var args = testFrameworkDiscovererAttribute.GetConstructorArguments().Cast <string>().ToArray();
            var testFrameworkDiscovererType = SerializationHelper.GetType(args[1], args[0]);

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

            return(GetTestFrameworkTypeDiscoverer(diagnosticMessageSink, testFrameworkDiscovererType));
        }
Пример #5
0
        /// <summary>
        /// Gets a test collection orderer, as specified in a reflected <see cref="TestCollectionOrdererAttribute"/>.
        /// </summary>
        /// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
        /// <param name="testCollectionOrdererAttribute">The test collection orderer attribute.</param>
        /// <returns>The test collection orderer, if the type is loadable; <c>null</c>, otherwise.</returns>
        public static ITestCollectionOrderer GetTestCollectionOrderer(IMessageSink diagnosticMessageSink, IAttributeInfo testCollectionOrdererAttribute)
        {
            var args        = testCollectionOrdererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var ordererType = SerializationHelper.GetType(args[1], args[0]);

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

            return(GetTestCollectionOrderer(diagnosticMessageSink, ordererType));
        }
Пример #6
0
        static Type GetTestCollectionFactoryType(_IAttributeInfo?collectionBehaviorAttribute)
        {
            if (collectionBehaviorAttribute == null)
            {
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

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

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

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

                return(typeof(CollectionPerClassTestCollectionFactory));
            }
            else if (ctorArgs.Count == 1 && ctorArgs[0] is Type factoryType)
            {
                if (!typeof(IXunitTestCollectionFactory).IsAssignableFrom(factoryType))
                {
                    TestContext.Current?.SendDiagnosticMessage("Test collection factory type '{0}' does not implement IXunitTestCollectionFactory", factoryType.FullName);
                }
                else
                {
                    return(factoryType);
                }
            }
            else if (ctorArgs.Count == 2 && ctorArgs[0] is string typeName && ctorArgs[1] is string assemblyName)
            {
                var result = SerializationHelper.GetType(assemblyName, typeName);
                if (result == null)
                {
                    TestContext.Current?.SendDiagnosticMessage("Unable to create test collection factory type '{0}, {1}'", assemblyName, typeName);
                }
                else
                {
                    if (!typeof(IXunitTestCollectionFactory).IsAssignableFrom(result))
                    {
                        TestContext.Current?.SendDiagnosticMessage("Test collection factory type '{0}, {1}' does not implement IXunitTestCollectionFactory", assemblyName, typeName);
                    }
                    else
                    {
                        return(result);
                    }
                }
            }
Пример #7
0
        /// <inheritdoc/>
        public IEnumerable <IAttributeInfo> GetCustomAttributes(string assemblyQualifiedAttributeTypeName)
        {
            var attributeType = SerializationHelper.GetType(assemblyQualifiedAttributeTypeName);

            Guard.ArgumentValid("assemblyQualifiedAttributeTypeName", "Could not locate type name", attributeType != null);

            return(Assembly.CustomAttributes
                   .Where(attr => attributeType.GetTypeInfo().IsAssignableFrom(attr.AttributeType.GetTypeInfo()))
                   .OrderBy(attr => attr.AttributeType.Name)
                   .Select(Reflector.Wrap)
                   .Cast <IAttributeInfo>()
                   .ToList());
        }
        /// <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 = SerializationHelper.GetType(args[1], args[0]);
                    var discoverer     = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererType);

                    IEnumerable <object[]> data = discoverer.GetData(dataAttribute, TestCase.TestMethod.Method);
                    if (data == null)
                    {
                        Aggregator.Add(new InvalidOperationException($"Test data returned null for {TestCase.TestMethod.TestClass.Class.Name}.{TestCase.TestMethod.Method.Name}. Make sure it is statically initialized before this test method is called."));
                        continue;
                    }
                    foreach (var dataRow in data)
                    {
                        toDispose.AddRange(dataRow.OfType <IDisposable>());

                        ITypeInfo[] resolvedTypes    = null;
                        var         methodToRun      = TestMethod;
                        var         convertedDataRow = methodToRun.ResolveMethodArguments(dataRow);

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

                        var parameterTypes = methodToRun.GetParameters().Select(p => p.ParameterType).ToArray();
                        convertedDataRow = Reflector.ConvertArguments(convertedDataRow, parameterTypes);

                        var theoryDisplayName = TestCase.TestMethod.Method.GetDisplayNameWithArguments(DisplayName, convertedDataRow, resolvedTypes);
                        var test       = new XunitTest(TestCase, theoryDisplayName);
                        var skipReason = SkipReason ?? dataAttribute.GetNamedArgument <string>("Skip");
                        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;
            }
        }
Пример #9
0
        /// <inheritdoc/>
        public virtual void Deserialize(IXunitSerializationInfo info)
        {
            DisplayName  = info.GetValue <string>("DisplayName");
            TestAssembly = info.GetValue <ITestAssembly>("TestAssembly");
            UniqueID     = Guid.Parse(info.GetValue <string>("UniqueID"));

            var assemblyName = info.GetValue <string>("DeclarationAssemblyName");
            var typeName     = info.GetValue <string>("DeclarationTypeName");

            if (!string.IsNullOrWhiteSpace(assemblyName) && !string.IsNullOrWhiteSpace(typeName))
            {
                CollectionDefinition = Reflector.Wrap(SerializationHelper.GetType(assemblyName, typeName));
            }
        }
        /// <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, ITestFrameworkDiscoveryOptions discoveryOptions)
        {
            var factAttributes = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).CastOrToList();

            if (factAttributes.Count > 1)
            {
                var message  = string.Format("Test method '{0}.{1}' has multiple [Fact]-derived attributes", testMethod.TestClass.Class.Name, testMethod.Method.Name);
                var testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, testMethod, message);
                return(ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus));
            }

            var factAttribute = factAttributes.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 = SerializationHelper.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(discoveryOptions, testMethod, factAttribute))
            {
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <inheritdoc/>
        public Type?GetTestFrameworkType(_IAttributeInfo attribute)
        {
            Guard.ArgumentNotNull(nameof(attribute), attribute);

            var args = attribute.GetConstructorArguments().ToArray();

            if (args.Length == 1)
            {
                return((Type)args[0] !);
            }

            var stringArgs = args.Cast <string>().ToArray();

            return(SerializationHelper.GetType(stringArgs[1], stringArgs[0]));
        }
Пример #12
0
        static Type GetTestCollectionFactoryType(IMessageSink diagnosticMessageSink, 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 = SerializationHelper.GetType((string)ctorArgs[1], (string)ctorArgs[0]);

            if (result == null)
            {
                diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Unable to create test collection factory type '{ctorArgs[1]}, {ctorArgs[0]}'"));
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            var resultTypeInfo = result.GetTypeInfo();

            if (!typeof(IXunitTestCollectionFactory).GetTypeInfo().IsAssignableFrom(resultTypeInfo))
            {
                diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Test collection factory type '{ctorArgs[1]}, {ctorArgs[0]}' does not implement IXunitTestCollectionFactory"));
                return(typeof(CollectionPerClassTestCollectionFactory));
            }

            return(result);
        }
Пример #13
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, ITestFrameworkDiscoveryOptions 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 = SerializationHelper.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(discoveryOptions, testMethod, factAttribute))
            {
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #14
0
        internal static IEnumerable <IAttributeInfo> GetCustomAttributes(Type type, string assemblyQualifiedAttributeTypeName)
        {
            Type attributeType = SerializationHelper.GetType(assemblyQualifiedAttributeTypeName);

            return(GetCustomAttributes(type, attributeType, GetAttributeUsage(attributeType)));
        }
Пример #15
0
        /// <inheritdoc/>
        public Type GetTestFrameworkType(IAttributeInfo attribute)
        {
            var args = attribute.GetConstructorArguments().Cast <string>().ToArray();

            return(SerializationHelper.GetType(args[1], args[0]));
        }
Пример #16
0
        internal static Type?GetType(string assemblyQualifiedAttributeTypeName)
        {
            Guard.ArgumentNotNull(nameof(assemblyQualifiedAttributeTypeName), assemblyQualifiedAttributeTypeName);

            return(attributeTypeCache.GetOrAdd(assemblyQualifiedAttributeTypeName, name => SerializationHelper.GetType(name)));
        }
Пример #17
0
 internal static Type GetType(string assemblyQualifiedAttributeTypeName)
 {
     return(attributeTypeCache.GetOrAdd(assemblyQualifiedAttributeTypeName, name => SerializationHelper.GetType(name)));
 }