Exemplo n.º 1
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 (IAttributeInfo traitAttribute in Method.GetCustomAttributes(typeof(TraitAttribute))
                                                            .Concat(Class.GetCustomAttributes(typeof(TraitAttribute))))
            {
                var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
                Traits.Add((string)ctorArgs[0], (string)ctorArgs[1]);
            }

            uniqueID = new Lazy<string>(GetUniqueID, true);
        }
Exemplo n.º 2
0
        public new IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite)
        {
            var asyncTest = method.GetCustomAttributes <AsyncTestAttribute>(true).FirstOrDefault();

            if (asyncTest == null)
            {
                var ret = new TestMethod(method, suite);

                ret.RunState = RunState.NotRunnable;
                ret.Properties.Set("_SKIPREASON", "Method marked with AsyncTestCase must have AsyncTest also applied");

                yield return(ret);

                yield break;
            }

            var baseParameters = BaseGetParametersForTestCase(method);
            var builder        = new NUnitTestCaseBuilder();

            baseParameters.HasExpectedResult = true;
            var newMethod = builder.BuildTestMethod(new EnumeratorTaskWrapper(method, asyncTest.Timeout), suite, baseParameters);

            // Unity takes arguments for test method from OriginalArguments (incorrect) instead of Arguments (correct)
            // We recreate TestCaseParameters, so Arguments from baseParameters become OriginalArguments in newMethod.params
            newMethod.parms = new TestCaseParameters(baseParameters);
            newMethod.parms.HasExpectedResult = false;
            yield return(newMethod);
        }
        public new IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite)
        {
            var asyncTest = method.GetCustomAttributes <AsyncTestAttribute>(true).FirstOrDefault();

            if (asyncTest == null)
            {
                var ret = new TestMethod(method, suite);

                ret.RunState = RunState.NotRunnable;
                ret.Properties.Set("_SKIPREASON", "Method marked with AsyncTestCaseSource must have AsyncTest also applied");

                yield return(ret);

                yield break;
            }

            var tests   = base.BuildFrom(method, suite);
            var builder = new NUnitTestCaseBuilder();

            foreach (var m in tests)
            {
                m.parms.HasExpectedResult = true;
                var newMethod = builder.BuildTestMethod(new EnumeratorTaskWrapper(m.Method, asyncTest.Timeout), suite, m.parms);
                m.parms.HasExpectedResult = false;
                yield return(newMethod);
            }
        }
Exemplo n.º 4
0
        public static string GetSkipReason(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                return attribute.GetPropertyValue<string>("Skip");

            return null;
        }
Exemplo n.º 5
0
        public static string GetDisplayName(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                return attribute.GetPropertyValue<string>("Name");

            return null;
        }
 private Test BuildSingleTestMethod(IMethodInfo method, Test? suite)
 {
     var builders = method.GetCustomAttributes<ISimpleTestBuilder>(false);
     return builders.Length > 0
         ? builders[0].BuildFrom(method, suite)
         : _nunitTestCaseBuilder.BuildTestMethod(method, suite, null);
 }
Exemplo n.º 7
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 (IAttributeInfo traitAttribute in Method.GetCustomAttributes(typeof(TraitAttribute))
                     .Concat(Class.GetCustomAttributes(typeof(TraitAttribute))))
            {
                var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
                Traits.Add((string)ctorArgs[0], (string)ctorArgs[1]);
            }

            uniqueID = new Lazy <string>(GetUniqueID, true);
        }
        public Test BuildFrom(IMethodInfo method, Test?parentSuite)
        {
            var tests = new List <TestMethod>();

            List <ITestBuilder> builders = new List <ITestBuilder>(
                method.GetCustomAttributes <ITestBuilder>(false));

            // See if we need to add a CombinatorialAttribute for parameterized data
            if (method.MethodInfo.GetParameters().Any(param => param.HasAttribute <IParameterDataSource>(false)) &&
                !builders.Any(builder => builder is CombiningStrategyAttribute))
            {
                builders.Add(new CombinatorialAttribute());
            }

            foreach (var attr in builders)
            {
                foreach (var test in attr.BuildFrom(method, parentSuite))
                {
                    tests.Add(test);
                }
            }

            return(builders.Count > 0 && method.GetParameters().Length > 0 || tests.Count > 0
                ? BuildParameterizedMethodSuite(method, tests)
                : BuildSingleTestMethod(method, parentSuite));
        }
        private static IEnumerable <VariantAttribute> GetCombinations(IMethodInfo method)
        {
            var methodVariants
                = method
                  .GetCustomAttributes(typeof(VariantAttribute))
                  .Select(a => new VariantAttribute((DatabaseProvider)a.GetConstructorArguments().First(), (ProgrammingLanguage)a.GetConstructorArguments().Skip(1).First())
            {
                Slow = a.GetNamedArgument <bool>(nameof(VariantAttribute.Slow))
            })
                  .ToList();

            if (methodVariants.Any())
            {
                return(methodVariants);
            }

            var typeVariants
                = method.Type
                  .GetCustomAttributes(typeof(VariantAttribute))
                  .Select(a => new VariantAttribute((DatabaseProvider)a.GetConstructorArguments().First(), (ProgrammingLanguage)a.GetConstructorArguments().Skip(1).First())
            {
                Slow = a.GetNamedArgument <bool>(nameof(VariantAttribute.Slow))
            })
                  .ToList();

            if (typeVariants.Any())
            {
                return(typeVariants);
            }

            return(new[] { new VariantAttribute(DatabaseProvider.SqlClient, ProgrammingLanguage.CSharp) });
        }
Exemplo n.º 10
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 discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(discovererAttribute);
                if (discoverer != null)
                    foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                        Traits.Add(keyValuePair.Key, keyValuePair.Value);
            }

            uniqueID = new Lazy<string>(GetUniqueID, true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Finds the tests on a test method.
        /// </summary>
        /// <param name="testCollection">The test collection that the test method belongs to.</param>
        /// <param name="type">The test class that the test method belongs to.</param>
        /// <param name="method">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(ITestCollection testCollection, ITypeInfo type, IMethodInfo method, bool includeSourceInformation, IMessageBus messageBus)
        {
            var factAttribute = 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(testCollection, AssemblyInfo, type, method, factAttribute))
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                    return false;

            return true;
        }
Exemplo n.º 12
0
        public static int GetTimeoutParameter(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                return attribute.GetPropertyValue<int>("Timeout");

            return -1;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the test commands for a test method.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>The <see cref="ITestCommand"/> objects for the test method</returns>
        public static IEnumerable<ITestCommand> GetTestCommands(IMethodInfo method)
        {
            Guard.ArgumentNotNull("method", method);

            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
                foreach (ITestCommand command in attribute.GetInstance<FactAttribute>().CreateTestCommands(method))
                    yield return command;
        }
Exemplo n.º 14
0
        public static MultiValueDictionary <string, string> GetTraits(this IMethodInfo methodInfo)
        {
            var attributes = methodInfo.GetCustomAttributes(typeof(TraitAttribute));
            var traits     = GetTraitsFromAttributes(attributes);

            attributes = methodInfo.Class.GetCustomAttributes(typeof(TraitAttribute));
            return(GetTraitsFromAttributes(attributes, traits));
        }
Exemplo n.º 15
0
        public static OneToSetMap <string, string> GetTraits(this IMethodInfo methodInfo)
        {
            // We only need to get the traits of the method. When we update the element,
            // we merge in the traits of the parent element
            var attributes = methodInfo.GetCustomAttributes(typeof(TraitAttribute));

            return(GetTraitsFromAttributes(attributes));
        }
Exemplo n.º 16
0
        public static string GetSkipReason(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
            {
                return(attribute.GetPropertyValue <string>("Skip"));
            }

            return(null);
        }
Exemplo n.º 17
0
        public static string GetDisplayName(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
            {
                return(attribute.GetPropertyValue <string>("Name"));
            }

            return(null);
        }
Exemplo n.º 18
0
    public IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
    {
        var myFact = testMethod.GetCustomAttributes(typeof(MyFactAttribute)).FirstOrDefault();

        if (myFact != null)
        {
            yield return(new MyTestCommand(testMethod, "Hello from MyTestClassCommand!", 0));
        }
    }
Exemplo n.º 19
0
        public static int GetTimeoutParameter(IMethodInfo method)
        {
            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
            {
                return(attribute.GetPropertyValue <int>("Timeout"));
            }

            return(-1);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Gets the traits on a test method.
        /// </summary>
        /// <param name="method">The method to be inspected</param>
        /// <returns>A dictionary of the traits</returns>
        public static MultiValueDictionary<string, string> GetTraits(IMethodInfo method)
        {
            var traits = new MultiValueDictionary<string, string>();

            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(TraitAttribute)))
                traits.AddValue(attribute.GetPropertyValue<string>("Name"),
                                attribute.GetPropertyValue<string>("Value"));

            return traits;
        }
                public IEnumerable <IAttributeInfo> GetCustomAttributes(string assemblyQualifiedAttributeTypeName)
                {
                    if (_attributes == null)
                    {
                        return(_inner.GetCustomAttributes(assemblyQualifiedAttributeTypeName));
                    }
                    var type = System.Type.GetType(assemblyQualifiedAttributeTypeName, true);

                    return(_attributes.Where(x => type.GetTypeInfo().IsAssignableFrom(x.Attribute.GetType().GetTypeInfo())));
                }
Exemplo n.º 22
0
        private static int GetPriority(IMethodInfo method)
        {
            var priorityAttribute = method
                                    .GetCustomAttributes(typeof(TestPriorityAttribute))
                                    .FirstOrDefault();

            return(priorityAttribute == null
                ? 0
                : priorityAttribute.GetPropertyValue <int>("Priority"));
        }
Exemplo n.º 23
0
 public static IEnumerable <ITestCommand> GetTestCommands(IMethodInfo method)
 {
     foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(FactAttribute)))
     {
         foreach (ITestCommand command in attribute.GetInstance <FactAttribute>().CreateTestCommands(method))
         {
             yield return(command);
         }
     }
 }
Exemplo n.º 24
0
        /// <summary>
        /// Return the first found custom attribute on a method
        /// </summary>
        /// <param name="attributeType">type of the custom attribute</param>
        /// <returns>the first found custom attribute; or null none is found</returns>
        public static T GetFirstCustomAttribute <T>(this IMethodInfo me) where T : Attribute
        {
            IAttributeInfo attrInfo = null;

            foreach (var a in me.GetCustomAttributes(typeof(T)))
            {
                attrInfo = a;
                break;
            }

            return(attrInfo != null?attrInfo.GetInstance <T>() : null);
        }
        /// <inheritdoc/>
        public ITestCollection Get(IMethodInfo testMethod)
        {
            string collectionName;
            var collectionAttribute = testMethod.GetCustomAttributes(typeof(CollectionAttribute)).SingleOrDefault();

            if (collectionAttribute == null)
                collectionName = "Test collection for " + testMethod.Type.Name + "." + testMethod.Name;
            else
                collectionName = (string)collectionAttribute.GetConstructorArguments().First();

            return testCollections.GetOrAdd(collectionName, CreateCollection);
        }
Exemplo n.º 26
0
    // Helpers

    private static Dictionary <string, List <string> > GetTraits(IMethodInfo method)
    {
        var result = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);

        foreach (var traitAttribute in method.GetCustomAttributes(typeof(TraitAttribute)))
        {
            var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
            result.Add((string)ctorArgs[0], (string)ctorArgs[1]);
        }

        return(result);
    }
Exemplo n.º 27
0
        protected override IEnumerable <string> GetDependencies(ITestCase testCase)
        {
            IMethodInfo testMethodInfo = testCase.TestMethod.Method;

            if (testMethodInfo == null)
            {
                return(null);
            }
            IEnumerable <IAttributeInfo> attributes = testMethodInfo.GetCustomAttributes(typeof(DependsOnTestAttribute));

            return(attributes.Select(x => x.GetConstructorArguments().OfType <string>().First()));
        }
Exemplo n.º 28
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) });
            }
        }
    }
Exemplo n.º 29
0
 public IEnumerable <IAttributeInfo> GetCustomAttributes(Type attributeType)
 {
     //
     if (attributeType == typeof(TraitAttribute))
     {
         yield return(new AttributeInfoWithDefaultVAlue(_sampleTraitAttrib));
     }
     else
     {
         foreach (IAttributeInfo attr in _originalMethodInfo.GetCustomAttributes(attributeType))
         {
             yield return(attr);
         }
     }
 }
Exemplo n.º 30
0
        /// <inheritdoc/>
        public IEnumerable<XunitTestCase> 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())
                {
                    List<XunitTestCase> 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]);
                        IDataDiscoverer discoverer = (IDataDiscoverer)Activator.CreateInstance(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) };
            }
        }
Exemplo n.º 31
0
        public static MultiValueDictionary <string, string> GetTraits(IMethodInfo method)
        {
            var traits = new MultiValueDictionary <string, string>();

            foreach (IAttributeInfo attribute in method.GetCustomAttributes(typeof(TraitAttribute)))
            {
                traits.AddValue(attribute.GetPropertyValue <string>("Name"),
                                attribute.GetPropertyValue <string>("Value"));
            }

            foreach (IAttributeInfo attribute in method.Class.GetCustomAttributes(typeof(TraitAttribute)))
            {
                traits.AddValue(attribute.GetPropertyValue <string>("Name"),
                                attribute.GetPropertyValue <string>("Value"));
            }

            return(traits);
        }
Exemplo n.º 32
0
        private string GetDescription(IMethodInfo member)
        {
            var attrib = member.GetCustomAttributes(typeof(DescriptionAttribute)).Cast <DescriptionAttribute>().FirstOrDefault();

            if (attrib == null)
            {
                return(member.Name);
            }

            var prop = attrib.Description;

            if (prop == null)
            {
                return(member.Name);
            }

            return(prop.ToString());
        }
Exemplo n.º 33
0
        /// <summary>
        /// Build a Test from the provided MethodInfo. Depending on
        /// whether the method takes arguments and on the availability
        /// of test case data, this method may return a single test
        /// or a group of tests contained in a ParameterizedMethodSuite.
        /// </summary>
        /// <param name="method">The method for which a test is to be built</param>
        /// <param name="parentSuite">The test fixture being populated, or null</param>
        /// <returns>A Test representing one or more method invocations</returns>
        public Test BuildFrom(IMethodInfo method, Test parentSuite)
        {
            var tests = new List <TestMethod>();

            List <ITestBuilder> builders = new List <ITestBuilder>(
                method.GetCustomAttributes <ITestBuilder>(false));

            // See if we need a CombinatorialAttribute added
            bool needCombinatorial = true;

            foreach (var attr in builders)
            {
                if (attr is CombiningStrategyAttribute)
                {
                    needCombinatorial = false;
                }
            }

            // This check must be done before CombinatorialAttribute gets added to the builders collection
            var hasBuildersSpecified = builders.Count > 0;

            // We could check to see if here are any data attributes specified
            // on the parameters but that's what CombinatorialAttribute does
            // and it simply won't return any cases if it finds nothing.
            // TODO: We need to add some other ITestBuilder than a combinatorial attribute
            // because we want the attribute to generate an error if it's present on
            // a generic method.
            if (needCombinatorial)
            {
                builders.Add(new CombinatorialAttribute());
            }

            foreach (var attr in builders)
            {
                foreach (var test in attr.BuildFrom(method, parentSuite))
                {
                    tests.Add(test);
                }
            }

            return(hasBuildersSpecified && method.GetParameters().Length > 0 || tests.Count > 0
                ? BuildParameterizedMethodSuite(method, tests)
                : BuildSingleTestMethod(method, parentSuite));
        }
Exemplo n.º 34
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);
        }
Exemplo n.º 35
0
        /// <summary>
        /// Finds the tests on a test method.
        /// </summary>
        /// <param name="testCollection">The test collection that the test method belongs to.</param>
        /// <param name="type">The test class that the test method belongs to.</param>
        /// <param name="method">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(ITestCollection testCollection, ITypeInfo type, IMethodInfo method, bool includeSourceInformation, IMessageBus messageBus)
        {
            var factAttribute = 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(testCollection, AssemblyInfo, type, method, factAttribute))
            {
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
    public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
    {
      foreach (var attr in testMethod.GetCustomAttributes(typeof(FactAttribute)))
      {
          if (!String.IsNullOrEmpty(attr.GetPropertyValue<String>("Skip")))
          {
              yield break;
          }
      }

      foreach (var testCommand in _cmd.EnumerateTestCommands(testMethod))
      {
        if (testCommand is MediumTrustTestCommand)
        {
          yield return testCommand;
          continue;
        }

        yield return new MediumTrustTestCommand(testCommand, null);
      }
    }
        public IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
        {
            foreach (var attr in testMethod.GetCustomAttributes(typeof(FactAttribute)))
            {
                if (!String.IsNullOrEmpty(attr.GetPropertyValue <String>("Skip")))
                {
                    yield break;
                }
            }

            foreach (var testCommand in _cmd.EnumerateTestCommands(testMethod))
            {
                if (testCommand is MediumTrustTestCommand)
                {
                    yield return(testCommand);

                    continue;
                }

                yield return(new MediumTrustTestCommand(testCommand, null));
            }
        }
Exemplo n.º 38
0
        private bool ValidateRequirements(IMethodInfo method)
        {
            if (method.HasAttribute(typeof(RuntimeTestAttribute)))
            {
                RuntimeTestAttribute attr = method.GetCustomAttributes(typeof(RuntimeTestAttribute)).First().GetInstance<RuntimeTestAttribute>();
                if (!(RuntimeTestAttribute.RuntimeTestsEnabled || Debugger.IsAttached))
                {
                    this.Skip = String.Format("Runtime tests are not enabled on this test environment. To enable runtime tests set the environment variable '{0}'=true or run the tests under a debugger.", RuntimeTestAttribute.RuntimeTestsEnabledEnvironmentVariable);
                    return false;
                }
                else if (!(attr.NonPrivileged || UacUtilities.IsProcessElevated))
                {
                    this.Skip = String.Format("The runtime test '{0}' requires that the test process be elevated.", method.Name);
                    return false;
                }
            }
            else if (method.HasAttribute(typeof(Is64BitSpecificTestAttribute)) && !Is64BitSpecificTestAttribute.Is64BitOperatingSystem)
            {
                this.Skip = "64-bit specific tests are not enabled on 32-bit machines.";
                return false;
            }

            return true;
        }
Exemplo n.º 39
0
        private bool ValidateRequirements(IMethodInfo method)
        {
            if (method.HasAttribute(typeof(RuntimeTestAttribute)))
            {
                RuntimeTestAttribute attr = method.GetCustomAttributes(typeof(RuntimeTestAttribute)).First().GetInstance <RuntimeTestAttribute>();
                if (!(RuntimeTestAttribute.RuntimeTestsEnabled || Debugger.IsAttached))
                {
                    this.Skip = String.Format("Runtime tests are not enabled on this test environment. To enable runtime tests set the environment variable '{0}'=true or run the tests under a debugger.", RuntimeTestAttribute.RuntimeTestsEnabledEnvironmentVariable);
                    return(false);
                }
                else if (!(attr.NonPrivileged || UacUtilities.IsProcessElevated))
                {
                    this.Skip = String.Format("The runtime test '{0}' requires that the test process be elevated.", method.Name);
                    return(false);
                }
            }
            else if (method.HasAttribute(typeof(Is64BitSpecificTestAttribute)) && !Is64BitSpecificTestAttribute.Is64BitOperatingSystem)
            {
                this.Skip = "64-bit specific tests are not enabled on 32-bit machines.";
                return(false);
            }

            return(true);
        }
Exemplo n.º 40
0
 /// <summary>
 /// Gets all the custom attributes for the method that are of the given type.
 /// </summary>
 /// <param name="methodInfo">The method.</param>
 /// <param name="attributeType">The type of the attribute.</param>
 /// <returns>The matching attributes that decorate the method.</returns>
 public static IEnumerable <IAttributeInfo> GetCustomAttributes(this IMethodInfo methodInfo, Type attributeType)
 {
     return(methodInfo.GetCustomAttributes(attributeType.AssemblyQualifiedName));
 }
Exemplo n.º 41
0
 /// <summary>
 /// Build a simple, non-parameterized TestMethod for this method.
 /// </summary>
 /// <param name="method">The MethodInfo for which a test is to be built</param>
 /// <param name="suite">The test suite for which the method is being built</param>
 /// <returns>A TestMethod.</returns>
 private Test BuildSingleTestMethod(IMethodInfo method, Test suite)
 {
     var builders = method.GetCustomAttributes<ISimpleTestBuilder>(false);
     return builders.Length > 0
         ? builders[0].BuildFrom(method, suite)
         : _nunitTestCaseBuilder.BuildTestMethod(method, suite, null);
 }
Exemplo n.º 42
0
        /// <summary>
        /// Build a Test from the provided MethodInfo. Depending on
        /// whether the method takes arguments and on the availability
        /// of test case data, this method may return a single test
        /// or a group of tests contained in a ParameterizedMethodSuite.
        /// </summary>
        /// <param name="method">The method for which a test is to be built</param>
        /// <param name="parentSuite">The test fixture being populated, or null</param>
        /// <returns>A Test representing one or more method invocations</returns>
        public Test BuildFrom(IMethodInfo method, Test parentSuite)
        {
            var tests = new List<TestMethod>();

            List<ITestBuilder> builders = new List<ITestBuilder>(
                method.GetCustomAttributes<ITestBuilder>(false));

            // See if we need a CombinatorialAttribute added
            bool needCombinatorial = true;
            foreach (var attr in builders)
            {
                if (attr is CombiningStrategyAttribute)
                    needCombinatorial = false;
            }

            // We could check to see if here are any data attributes specified
            // on the parameters but that's what CombinatorialAttribute does
            // and it simply won't return any cases if it finds nothing.
            // TODO: We need to add some other ITestBuilder than a combinatorial attribute
            // because we want the attribute to generate an error if it's present on
            // a generic method.
            if (needCombinatorial)
                builders.Add(new CombinatorialAttribute());

            foreach (var attr in builders)
            {
                foreach (var test in attr.BuildFrom(method, parentSuite))
                    tests.Add(test);
            }

            return tests.Count > 0
                ? BuildParameterizedMethodSuite(method, tests)
                : BuildSingleTestMethod(method, parentSuite);
        }
Exemplo n.º 43
0
 public T[] GetCustomAttributes <T>(bool inherit) where T : class
 {
     return(_baseInfo.GetCustomAttributes <T>(inherit)
            .Concat(_extraAttributes.OfType <T>())
            .ToArray());
 }
 private static bool IsSpecFor(IMethodInfo testMethod, Type type)
 {
     if (!IsSpec(testMethod))
         return false;
     return testMethod.GetCustomAttributes(typeof(SpecForAttribute)).ToList()
         .Exists(attr => attr.GetPropertyValue<Type[]>("TransitionTypes").Contains(type));
 }
Exemplo n.º 45
0
            public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite)
            {
                var explic = method.GetCustomAttributes <ExplicitAttribute>(true)
                             .Cast <IApplyToTest>()
                             .Union(method.GetCustomAttributes <IgnoreAttribute>(true))
                             .ToList();

                var builder = new NUnitTestCaseBuilder();

                TestMethod test    = null;
                var        hasTest = false;

                foreach (var provider in _providerNames)
                {
                    var isIgnore = !UserProviders.ContainsKey(provider);

                    foreach (var parameters in GetParameters(provider))
                    {
                        var data = new TestCaseParameters(parameters);

                        test = builder.BuildTestMethod(method, suite, data);

                        foreach (var attr in explic)
                        {
                            attr.ApplyToTest(test);
                        }

                        test.Properties.Set(PropertyNames.Order, GetOrder(method));
                        //test.Properties.Set(PropertyNames.ParallelScope, ParallelScope);
                        test.Properties.Set(PropertyNames.Category, provider);

                        SetName(test, method, provider, false);

                        if (isIgnore)
                        {
                            if (test.RunState != RunState.NotRunnable && test.RunState != RunState.Explicit)
                            {
                                test.RunState = RunState.Ignored;
                            }

                            test.Properties.Set(PropertyNames.SkipReason, "Provider is disabled. See UserDataProviders.txt");
                            continue;
                        }

                        hasTest = true;
                        yield return(test);
                    }

#if !NETSTANDARD && !MONO
                    if (!isIgnore && _includeLinqService)
                    {
                        foreach (var paremeters in GetParameters(provider + ".LinqService"))
                        {
                            var data = new TestCaseParameters(paremeters);
                            test = builder.BuildTestMethod(method, suite, data);

                            foreach (var attr in explic)
                            {
                                attr.ApplyToTest(test);
                            }

                            test.Properties.Set(PropertyNames.Order, GetOrder(method));
                            //test.Properties.Set(PropertyNames.ParallelScope, ParallelScope);
                            test.Properties.Set(PropertyNames.Category, provider);

                            SetName(test, method, provider, true);

                            yield return(test);
                        }
                    }
#endif
                }

                if (!hasTest)
                {
                    yield return(test);
                }
            }
Exemplo n.º 46
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;

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

            foreach (IAttributeInfo traitAttribute in Method.GetCustomAttributes(typeof(TraitAttribute)))
            {
                var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
                Traits.Add((string)ctorArgs[0], (string)ctorArgs[1]);
            }

            uniqueID = new Lazy<string>(GetUniqueID, true);
        }