Пример #1
0
        public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod,
                                                     IAttributeInfo factAttribute)
        {
            IXunitTestCase testCase;

            if (testMethod.Method.GetParameters().Any())
            {
                testCase = new ExecutionErrorTestCase(_messageSink, discoveryOptions.MethodDisplayOrDefault(),
                                                      discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod,
                                                      "[RetryFact] methods are not allowed to have parameters. Did you mean to use [RetryTheory]?");
            }
            else if (testMethod.Method.IsGenericMethodDefinition)
            {
                testCase = new ExecutionErrorTestCase(_messageSink, discoveryOptions.MethodDisplayOrDefault(),
                                                      discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod,
                                                      "[RetryFact] methods are not allowed to be generic.");
            }
            else
            {
                var maxRetries            = factAttribute.GetNamedArgument <int>(nameof(RetryFactAttribute.MaxRetries));
                var delayBetweenRetriesMs =
                    factAttribute.GetNamedArgument <int>(nameof(RetryFactAttribute.DelayBetweenRetriesMs));
                testCase = new RetryTestCase(_messageSink, discoveryOptions.MethodDisplayOrDefault(),
                                             discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, maxRetries, delayBetweenRetriesMs);
            }

            return(new[] { testCase });
        }
Пример #2
0
        /// <summary>
        /// Discover test cases from a test method. By default, if the method is generic, or
        /// it contains arguments, returns a single <see cref="ExecutionErrorTestCase"/>;
        /// otherwise, it returns the result of calling <see cref="CreateTestCase"/>.
        /// </summary>
        /// <param name="discoveryOptions">The discovery options to be used.</param>
        /// <param name="testMethod">The test method the test cases belong to.</param>
        /// <param name="factAttribute">The fact attribute attached to the test method.</param>
        /// <returns>Returns zero or more test cases represented by the test method.</returns>
        public virtual IEnumerable <IXunitTestCase> Discover(
            ITestFrameworkDiscoveryOptions discoveryOptions,
            ITestMethod testMethod,
            IAttributeInfo factAttribute)
        {
            if (testMethod is null)
            {
                throw new System.ArgumentNullException(nameof(testMethod));
            }

            IXunitTestCase testCase;

            if (testMethod.Method.GetParameters().Any())
            {
                testCase = new ExecutionErrorTestCase(this.DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, "[Fact] methods are not allowed to have parameters. Did you mean to use [Theory]?");
            }
            else if (testMethod.Method.IsGenericMethodDefinition)
            {
                testCase = new ExecutionErrorTestCase(this.DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, "[Fact] methods are not allowed to be generic.");
            }
            else
            {
                testCase = this.CreateTestCase(discoveryOptions, testMethod, factAttribute);
            }

            return(new[] { testCase });
        }
        private bool FindTestsForMethodOnAutofacTestClass(ITestMethod testMethod,
                                                          bool includeSourceInformation,
                                                          IMessageBus messageBus,
                                                          ITestFrameworkDiscoveryOptions discoveryOptions)
        {
            var factAttributes = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).ToList();

            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.All, testMethod, message);
                return(ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus));
            }

            var factAttribute = factAttributes.FirstOrDefault();

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

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

            IXunitTestCaseDiscoverer discoverer = null;

            if (factAttributeType == typeof(FactAttribute))
            {
                discoverer = _autofacFactDiscoverer;
            }
            else if (factAttributeType == typeof(TheoryAttribute))
            {
                discoverer = _autofacTheoryDiscoverer;
            }

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

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

            return(true);
        }
Пример #4
0
        protected override bool FindTestsForMethod(
            ITestMethod testMethod,
            bool includeSourceInformation,
            IMessageBus messageBus,
            ITestFrameworkDiscoveryOptions discoveryOptions)
        {
            if (typeof(ILoggedTest).IsAssignableFrom(testMethod.TestClass.Class.ToRuntimeType()))
            {
                var factAttributes = testMethod.Method.GetCustomAttributes(typeof(FactAttribute));
                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();
                if (!Discoverers.TryGetValue(factAttributeType, out var discoverer))
                {
                    return(base.FindTestsForMethod(testMethod, includeSourceInformation, messageBus, discoveryOptions));
                }
                else
                {
                    foreach (var testCase in discoverer.Discover(discoveryOptions, testMethod, factAttribute))
                    {
                        if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            else
            {
                return(base.FindTestsForMethod(testMethod, includeSourceInformation, messageBus, discoveryOptions));
            }
        }
Пример #5
0
 internal static bool TryEvaluateSkipConditions(ITestFrameworkDiscoveryOptions discoveryOptions, IMessageSink diagnosticMessageSink, ITestMethod testMethod, object[] conditionArguments, out string skipReason, out ExecutionErrorTestCase errorTestCase)
 {
     skipReason    = null;
     errorTestCase = null;
     try
     {
         skipReason = EvaluateSkipConditions(testMethod, conditionArguments);
         return(true);
     }
     catch (ConditionalDiscovererException e)
     {
         errorTestCase = new ExecutionErrorTestCase(
             diagnosticMessageSink,
             discoveryOptions.MethodDisplayOrDefault(),
             discoveryOptions.MethodDisplayOptionsOrDefault(),
             testMethod,
             e.Message);
         return(false);
     }
 }
Пример #6
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);
        }