示例#1
0
        /// <summary>
        /// Runs the tests.
        /// </summary>
        /// <param name="assemblyName">Name of the assembly.</param>
        public void RunTests(string assemblyName)
        {
            // Set up the global results containers
            List<TestResult> testResults = new List<TestResult>();
            Stats stats = new Stats();
            List<IResultWriter> plugins = LoadResultPlugIns();

            Console.WriteLine("Loading Assemblies ...");
            try
            {
                // 1. Load the assembly.
                Assembly assembly = GetAssembly(GetFullAssemblyPath(assemblyName));

                // 2. Get test classes.
                Type[] allTypes = assembly.GetTypes();
                List<Type> testClasses = new List<Type>();
                foreach (var item in allTypes)
                {
                    if (CheckClassIsATestClass(CustomAttributeData.GetCustomAttributes(item)))
                    {
                        testClasses.Add(item);
                    }
                }

                if (testClasses == null || testClasses.Count() == 0)
                    return;

                // 3. Get test methods for each class.

                foreach (var current in testClasses)
                {
                    List<MethodInfo> testMethods = new List<MethodInfo>();
                    List<MethodInfo> classSetupMethods = new List<MethodInfo>();
                    List<MethodInfo> classTeardownMethods = new List<MethodInfo>();
                    List<MethodInfo> setupMethods = new List<MethodInfo>();
                    List<MethodInfo> teardownMethods = new List<MethodInfo>();

                    MethodInfo[] allMethods = current.GetMethods();

                    foreach (var item in allMethods)
                    {
                        if (CheckMethodIsAClassSetupMethod(CustomAttributeData.GetCustomAttributes(item)))
                        {
                            classSetupMethods.Add(item);
                        }

                        if (CheckMethodIsATestSetupMethod(CustomAttributeData.GetCustomAttributes(item)))
                        {
                            setupMethods.Add(item);
                        }

                        if (CheckMethodIsATestMethod(CustomAttributeData.GetCustomAttributes(item)))
                        {
                            testMethods.Add(item);
                        }

                        if (CheckMethodIsATestTeardownMethod(CustomAttributeData.GetCustomAttributes(item)))
                        {
                            teardownMethods.Add(item);
                        }

                        if (CheckMethodIsAClassTeardownMethod(CustomAttributeData.GetCustomAttributes(item)))
                        {
                            classTeardownMethods.Add(item);
                        }
                    }

                    if (testMethods == null || testMethods.Count() == 0)
                        continue;

                    Console.WriteLine("Loading " + current.FullName + "...");
                    Console.WriteLine("Starting execution, {0} tests to run...", testMethods.Count());

                    object instance = assembly.CreateInstance(current.FullName);

                    // 4. Run test methods.
                    ExecuteMethods(classSetupMethods, instance);
                    try
                    {
                        ExecuteTestMethods(stats, testResults, setupMethods, testMethods, teardownMethods, instance);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        ExecuteMethods(classTeardownMethods, instance);
                    }

                    Console.WriteLine();
                    Console.WriteLine(stats.GetFinalResult());
                }
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                // Unexpected error.
                Console.WriteLine("  An unexpected error occured: {0}", ex.Message);
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.Message == "Exception has been thrown by the target of an invocation.")
                    {
                        if (ex.InnerException.InnerException != null)
                        {
                            Console.WriteLine("  Reason: {0}", ex.InnerException.InnerException.Message);
                        }

                        else
                        {
                            Console.WriteLine("  Reason: {0}", ex.InnerException.Message);
                        }
                    }
                }
            }
            finally
            {
                foreach (IResultWriter item in plugins)
                {
                    item.WriteResult(assemblyName, testResults);
                }
            }
        }
示例#2
0
        private void ExecuteTestMethods(Stats stats, List<TestResult> testResults, List<MethodInfo> testSetupMethods, List<MethodInfo> testMethods, List<MethodInfo> testTeardownMethods, object instance)
        {
            foreach (var method in testMethods)
            {
                TestResult testResult = new TestResult();
                testResult.ClassName = instance.GetType().Name;
                testResult.TestName = method.Name;
                testResult.TestOutcome = "Failed";

                try
                {
                    stats.AddGlobalCount();

                    Console.WriteLine("Running test: {0}", method.Name);
                    stats.StartLocalTime();
                    ExecuteMethods(testSetupMethods, instance);
                    try
                    {
                        method.Invoke(instance, null);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        ExecuteMethods(testTeardownMethods, instance);
                    }

                    Console.WriteLine("  Passed ({0} s).", stats.LocalTime.TotalSeconds);
                    testResult.TestOutcome = "Passed";
                    testResult.Duration = stats.LocalTime;
                    stats.AddGlobalPassCount();
                }
                catch (Exception ex)
                {
                    stats.AddGlobalFailCount();
                    testResult.TestOutcome = "Failed";
                    string TestResultString = string.Empty;
                    // Check for a failing assert.
                    if (ex.InnerException != null)
                    {
                        string exceptionType = ex.InnerException.GetType().ToString();
                        switch (exceptionType)
                        {
                            case "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException":
                            case "NUnit.Framework.AssertionException":
                                testResult.TestOutcome = "Failed";
                                TestResultString = String.Format("  Failed: {0} ({1} s).", ex.InnerException.Message, stats.LocalTime.TotalSeconds);
                                stats.AddGlobalFailCount();
                                Console.WriteLine(TestResultString);
                                testResult.Messages = TestResultString;
                                testResult.Duration = stats.LocalTime;
                                break;

                            case "Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException":
                            case "NUnit.Framework.Assert.InconclusiveException":

                                testResult.TestOutcome = "Inconclusive";
                                TestResultString = String.Format("  Inconclusive: {0} ({1} s).", ex.InnerException.Message, stats.LocalTime.TotalSeconds);
                                stats.AddGlobalInconclusiveCount();
                                Console.WriteLine(TestResultString);
                                testResult.Messages = TestResultString;
                                testResult.Duration = stats.LocalTime;
                                break;
                            default:
                                break;
                        }
                        continue;
                    }

                    // Unexpected error.
                    TestResultString = String.Format("  An unexpected error occured: {0}", ex.Message);

                    if (ex.InnerException != null)
                    {
                        TestResultString = TestResultString + "\n" + string.Format("  Reason: {0}", ex.InnerException.Message);
                        Console.WriteLine(TestResultString);
                        testResult.Messages = TestResultString;
                    }
                }
                finally
                {
                    stats.ResetLocalTime();
                    testResults.Add(testResult);
                }
            }
        }