private int RunTests()

in isamunittests/IsamTestHelper.cs [1333:1504]


        private int RunTests(Func<MethodInfo, bool> selector)
        {
            int totalTests = 0;
            int failures = 0;
            var totalStopwatch = Stopwatch.StartNew();

            TypeInfo[] typeinfos = this.assembly.DefinedTypes.Where(MemberHasAttribute<TestClassAttribute>).ToArray();
            Type[] types = new Type[typeinfos.Length];

            for (int i = 0; i < typeinfos.Length; ++i)
            {
                types[i] = typeinfos[i].AsType();
            }

            if (this.ShuffleTests)
            {
                Shuffle(types);
            }

            foreach (Type type in types)
            {
                bool failed = false;

                // ConstructorInfo constructor = System.Reflection.Emit.TypeBuilder.GetConstructor(type, null);
                ConstructorInfo[] constructors = type.GetTypeInfo().DeclaredConstructors.ToArray();
                if (null == constructors || constructors.Length == 0)
                {
                    throw new InvalidOperationException(
                        string.Format("Type {0} does not have a constructor", type.FullName));
                }

                // ".cctor' also matches constructors with empty parameter lists.
                ConstructorInfo[] emptyConstructors = constructors
                    .Where(con => (con.GetParameters().Length == 0 && con.Name == ".ctor"))
                    .ToArray();
                if (emptyConstructors.Length > 1)
                {
                    throw new InvalidOperationException(
                        string.Format("Type {0} has too many constructors: {1}.", type.FullName, emptyConstructors.Length));
                }

                ConstructorInfo constructor = emptyConstructors[0];
                if (constructor.GetParameters().Length > 0)
                {
                    throw new InvalidOperationException(
                        string.Format("Type {0} does not have a default constructor.", type.FullName));
                }

                MethodInfo initialize = GetFirstMethodWithAttribute<TestInitializeAttribute>(type);
                MethodInfo cleanup = GetFirstMethodWithAttribute<TestCleanupAttribute>(type);

                MethodInfo[] methods = type.GetTypeInfo().DeclaredMethods.Where(MemberHasAttribute<TestMethodAttribute>).Where(selector).ToArray();

                if (this.ShuffleTests)
                {
                    Shuffle(methods);
                }

                InvokeMethod(null, GetFirstMethodWithAttribute<ClassInitializeAttribute>(type));
                try
                {
                    foreach (MethodInfo method in methods)
                    {
                        string description = GetTestMethodDescription(method);
                        Type expectedException = GetExpectedException(method);

                        Debug.WriteLine("{0}.{1}: ", type.Name, method.Name);

                        Debug.WriteLine(".......................................................................................");
                        Stopwatch stopwatch = Stopwatch.StartNew();

                        object obj = constructor.Invoke(null);
                        InvokeMethod(obj, initialize);
                        try
                        {
                            totalTests++;
                            InvokeMethod(obj, method);
                            if (null != expectedException)
                            {
                                failures++;
                                failed = true;
                                Debug.WriteLine(String.Empty);
                                Debug.WriteLine("Failure in {0}.{1}: {2}", type.Name, method.Name, description);
                                Debug.WriteLine("Expected exception {0} was not thrown", expectedException);

                                if (!this.CatchExceptions)
                                {
                                    Assert.Fail(String.Format("Expected exception {0} was not thrown", expectedException));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex is TargetInvocationException)
                            {
                                ex = ex.InnerException;
                            }

                            if (ex is AssertInconclusiveException)
                            {
                                Debug.WriteLine(String.Empty);
                                Debug.WriteLine("Assert.Inconclusive in {0}.{1}: {2}", type.Name, method.Name, description);
                                Debug.WriteLine("{0}", ex);
                            }
                            else if (null == expectedException)
                            {
                                failures++;
                                failed = true;
                                Debug.WriteLine(String.Empty);
                                Debug.WriteLine("Failure in {0}.{1}: {2}", type.Name, method.Name, description);
                                Debug.WriteLine("Unexpected exception {0}", ex);

                                if (!this.CatchExceptions)
                                {
                                    throw;
                                }
                            }
                            else if (!ex.GetType().Equals(expectedException) && !ex.GetType().GetTypeInfo().IsSubclassOf(expectedException))
                            {
                                failures++;
                                failed = true;
                                Debug.WriteLine(String.Empty);
                                Debug.WriteLine("Failure in {0}.{1}: {2}", type.Name, method.Name, description);
                                Debug.WriteLine("Unexpected exception {0}, expected {1}", ex, expectedException);

                                if (!this.CatchExceptions)
                                {
                                    throw;
                                }
                            }
                            else if (ex.GetType().Equals(expectedException))
                            {
                                Debug.WriteLine(String.Empty);
                                Debug.WriteLine("Exception {0} caught, and it was expected", ex);
                            }
                            else if (ex.GetType().GetTypeInfo().IsSubclassOf(expectedException))
                            {
                                Debug.WriteLine(String.Empty);
                                Debug.WriteLine("Exception {0} caught, subclass of expected {1}", ex, expectedException);
                            }
                        }
                        finally
                        {
                            if (!failed || this.CatchExceptions)
                            {
                                InvokeMethod(obj, cleanup);
                            }
                        }

                        stopwatch.Stop();
                        Debug.WriteLine("\tFinishes in {0}", stopwatch.Elapsed);
                    }
                }
                finally
                {
                    if (!failed || this.CatchExceptions)
                    {
                        InvokeMethod(null, GetFirstMethodWithAttribute<ClassCleanupAttribute>(type));
                    }
                }
            }

            totalStopwatch.Stop();
            Debug.WriteLine(string.Empty);
            Debug.WriteLine("{0} tests", totalTests);
            Debug.WriteLine("\t{0} failures", failures);
            Debug.WriteLine("\t{0}", totalStopwatch.Elapsed);

            this.failureCount += failures;

            return totalTests;
        }