Example #1
0
        /// <summary>
        /// Get the assembly info for the parameter type
        /// </summary>
        /// <param name="type"> The type. </param>
        /// <returns> The <see cref="TestAssemblyInfo"/> instance. </returns>
        private TestAssemblyInfo GetAssemblyInfo(Type type)
        {
            var assembly = type.GetTypeInfo().Assembly;

            if (!this.testAssemblyInfoCache.TryGetValue(assembly, out TestAssemblyInfo assemblyInfo))
            {
                var assemblyInitializeType = typeof(AssemblyInitializeAttribute);
                var assemblyCleanupType    = typeof(AssemblyCleanupAttribute);

                assemblyInfo = new TestAssemblyInfo(assembly);

                var types = new AssemblyEnumerator().GetTypes(assembly, assembly.FullName, null);

                foreach (var t in types)
                {
                    if (t == null)
                    {
                        continue;
                    }

                    try
                    {
                        // Only examine classes which are TestClass or derives from TestClass attribute
                        if (!this.reflectionHelper.IsAttributeDefined(t, typeof(TestClassAttribute), inherit: true) &&
                            !this.reflectionHelper.HasAttributeDerivedFrom(t, typeof(TestClassAttribute), true))
                        {
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        // If we fail to discover type from an assembly, then do not abort. Pick the next type.
                        PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning(
                            "TypeCache: Exception occurred while checking whether type {0} is a test class or not. {1}",
                            t.FullName,
                            ex);

                        continue;
                    }

                    // Enumerate through all methods and identify the Assembly Init and cleanup methods.
                    foreach (var methodInfo in t.GetTypeInfo().DeclaredMethods)
                    {
                        if (this.IsAssemblyOrClassInitializeMethod(methodInfo, assemblyInitializeType))
                        {
                            assemblyInfo.AssemblyInitializeMethod = methodInfo;
                        }
                        else if (this.IsAssemblyOrClassCleanupMethod(methodInfo, assemblyCleanupType))
                        {
                            assemblyInfo.AssemblyCleanupMethod = methodInfo;
                        }
                    }
                }

                assemblyInfo = this.testAssemblyInfoCache.GetOrAdd(assembly, assemblyInfo);
            }

            return(assemblyInfo);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestClassInfo"/> class.
        /// </summary>
        /// <param name="type">Underlying test class type.</param>
        /// <param name="constructor">Constructor for the test class.</param>
        /// <param name="testContextProperty">Reference to the <see cref="TestContext"/> property in test class.</param>
        /// <param name="classAttribute">Test class attribute.</param>
        /// <param name="parent">Parent assembly info.</param>
        internal TestClassInfo(
            Type type,
            ConstructorInfo constructor,
            PropertyInfo testContextProperty,
            TestClassAttribute classAttribute,
            TestAssemblyInfo parent)
        {
            Debug.Assert(type != null, "Type should not be null");
            Debug.Assert(constructor != null, "Constructor should not be null");
            Debug.Assert(parent != null, "Parent should not be null");
            Debug.Assert(classAttribute != null, "ClassAtribute should not be null");

            this.ClassType                      = type;
            this.Constructor                    = constructor;
            this.TestContextProperty            = testContextProperty;
            this.BaseTestInitializeMethodsQueue = new Queue <MethodInfo>();
            this.BaseTestCleanupMethodsQueue    = new Queue <MethodInfo>();
            this.Parent         = parent;
            this.ClassAttribute = classAttribute;
        }
Example #3
0
        private TestAssemblyInfo GetAssemblyInfo(Type type)
        {
            var assembly = type.GetTypeInfo().Assembly;
            TestAssemblyInfo assemblyInfo;

            if (!this.testAssemblyInfoCache.TryGetValue(assembly, out assemblyInfo))
            {
                // Aquiring a lock is usually a costly operation which does not need to be
                // performed every time if the assembly is found in the cache.
                lock (this.assemblyInfoSyncObject)
                {
                    if (this.testAssemblyInfoCache.TryGetValue(assembly, out assemblyInfo))
                    {
                        return(assemblyInfo);
                    }

                    var assemblyInitializeType = typeof(AssemblyInitializeAttribute);
                    var assemblyCleanupType    = typeof(AssemblyCleanupAttribute);

                    assemblyInfo = new TestAssemblyInfo();

                    var types = new AssemblyEnumerator().GetTypes(assembly, assembly.FullName, null);

                    foreach (var t in types)
                    {
                        if (t == null)
                        {
                            continue;
                        }

                        try
                        {
                            // Only examine test classes for test attributes
                            if (!this.reflectionHelper.IsAttributeDefined(t, typeof(TestClassAttribute), inherit: true))
                            {
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            // If we fail to discover type from an assembly, then do not abort. Pick the next type.
                            PlatformServiceProvider.Instance.AdapterTraceLogger.LogWarning(
                                "TypeCache: Exception occured while checking whether type {0} is a test class or not. {1}",
                                t.FullName,
                                ex);

                            continue;
                        }

                        // Enumerate through all methods and identify the Assembly Init and cleanup methods.
                        foreach (var methodInfo in t.GetTypeInfo().DeclaredMethods)
                        {
                            if (this.IsAssemblyOrClassInitializeMethod(methodInfo, assemblyInitializeType))
                            {
                                assemblyInfo.AssemblyInitializeMethod = methodInfo;
                            }
                            else if (this.IsAssemblyOrClassCleanupMethod(methodInfo, assemblyCleanupType))
                            {
                                assemblyInfo.AssemblyCleanupMethod = methodInfo;
                            }
                        }
                    }

                    this.testAssemblyInfoCache.Add(assembly, assemblyInfo);
                }
            }

            return(assemblyInfo);
        }