/// <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);
        }
Beispiel #2
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 internal virtual bool FindTestsForMethod(ITestMethod testMethod, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkDiscoveryOptions discoveryOptions)
        {
            Console.WriteLine("FindTestsForMethod");

            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";
#pragma warning disable CA2000 // Dispose objects before losing scope
                var testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod, message);
#pragma warning restore CA2000 // Dispose objects before losing scope
                return(ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus));
            }

            Type attributeType;
            var  factAttribute = factAttributes.FirstOrDefault();

            if (factAttribute == null)
            {
                var testCaseAttributes = testMethod.Method.GetCustomAttributes(typeof(TestCaseAttribute)).CastOrToList();
                if (testCaseAttributes.Count > 1)
                {
                    var message = $"Test method '{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}' has multiple [TestCase]-derived attributes";
#pragma warning disable CA2000 // Dispose objects before losing scope
                    var testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod, message);
#pragma warning restore CA2000 // Dispose objects before losing scope
                    return(ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus));
                }

                factAttribute = testCaseAttributes.FirstOrDefault();
                if (factAttribute == null)
                {
                    return(true);
                }
            }

            attributeType = (factAttribute as IReflectionAttributeInfo)?.Attribute.GetType();
            Type discovererType = null;

            if (attributeType == null || !DiscovererTypeCache.TryGetValue(attributeType, 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 (attributeType != null)
                {
                    DiscovererTypeCache[attributeType] = discovererType;
                }
            }

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

            // support backwards compatability of Fact/Theory
            // however, convert the test case to MettleTestCase.
            if (discovererType == typeof(Xunit.Sdk.FactDiscoverer))
            {
                discovererType = typeof(Mettle.Xunit.Sdk.FactDiscoverer);
            }

            // if(discovererType == typeof(TheoryDiscoverer))
            //    discovererType = typeof(MettleTheoryDiscoverer);
            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);
        }