Ejemplo n.º 1
0
        /// <summary>
        /// Run tests in a [TestClass]
        /// </summary>
        ///
        /// <returns>
        /// Whether everything in <paramref name="testClass"/> succeeded
        /// </returns>
        ///
        static public bool Run(TestClass testClass)
        {
            Guard.NotNull(testClass, "testClass");

            TestContext.FullyQualifiedTestClassName = testClass.FullName;
            try
            {
                WriteLine();
                WriteHeading(testClass.FullName);

                bool classInitializeSucceeded = false;
                int  ran     = 0;
                int  passed  = 0;
                int  failed  = 0;
                int  ignored = 0;
                bool classCleanupSucceeded = false;

                if (testClass.IsIgnored)
                {
                    WriteLine();
                    WriteLine("Ignoring all tests because class is decorated with [Ignore]");
                    ignored = testClass.TestMethods.Count;
                }
                else
                {
                    //
                    // Run [ClassInitialize] method
                    //
                    TestContext.TestName           = testClass.TestMethods.First().Name;
                    TestContext.CurrentTestOutcome = UnitTestOutcome.InProgress;

                    classInitializeSucceeded =
                        MethodRunner.Run(
                            testClass.ClassInitializeMethod, null,
                            true,
                            null, false,
                            "[ClassInitialize]");

                    TestContext.TestName           = null;
                    TestContext.CurrentTestOutcome = UnitTestOutcome.Unknown;

                    if (classInitializeSucceeded)
                    {
                        //
                        // Run [TestMethod]s
                        //
                        foreach (var testMethod in testClass.TestMethods)
                        {
                            switch (
                                TestMethodRunner.Run(
                                    testMethod,
                                    testClass.TestInitializeMethod,
                                    testClass.TestCleanupMethod,
                                    testClass))
                            {
                            case UnitTestOutcome.Passed:
                                passed++;
                                ran++;
                                break;

                            case UnitTestOutcome.Failed:
                                failed++;
                                ran++;
                                break;

                            case UnitTestOutcome.NotRunnable:
                                ignored++;
                                break;
                            }
                        }

                        //
                        // Run [ClassCleanup] method
                        //
                        classCleanupSucceeded =
                            MethodRunner.Run(
                                testClass.ClassCleanupMethod, null,
                                false,
                                null, false,
                                "[ClassCleanup]");
                    }
                }

                //
                // Print results
                //
                WriteSubheading("Summary");
                WriteLine();
                WriteLine("ClassInitialize: {0}",
                          testClass.ClassInitializeMethod == null
                        ? "Not present"
                        : classInitializeSucceeded
                            ? "Succeeded"
                            : "Failed");
                WriteLine("Total:           {0} tests", testClass.TestMethods.Count);
                WriteLine("Ran:             {0} tests", ran);
                WriteLine("Ignored:         {0} tests", ignored);
                WriteLine("Passed:          {0} tests", passed);
                WriteLine("Failed:          {0} tests", failed);
                WriteLine("ClassCleanup:    {0}",
                          testClass.ClassCleanupMethod == null
                        ? "Not present"
                        : classCleanupSucceeded
                            ? "Succeeded"
                            : "Failed");

                return
                    (classInitializeSucceeded &&
                     failed == 0 &&
                     classCleanupSucceeded);
            }
            finally
            {
                TestContext.FullyQualifiedTestClassName = null;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Run a test method (plus its intialize and cleanup methods, if present)
        /// </summary>
        ///
        /// <remarks>
        /// If the test method is decorated with [Ignore], nothing is run
        /// </remarks>
        ///
        /// <returns>
        /// The results of the test
        /// </returns>
        ///
        static public UnitTestOutcome Run(
            TestMethod testMethod,
            MethodInfo testInitializeMethod,
            MethodInfo testCleanupMethod,
            TestClass testClass)
        {
            TestContext.TestName = testMethod.Name;
            try
            {
                WriteSubheading(testMethod.Name.Replace("_", " "));

                if (testMethod.IsIgnored)
                {
                    WriteLine();
                    WriteLine("Ignored because method is decorated with [Ignore]");
                    return(UnitTestOutcome.NotRunnable);
                }

                //
                // Construct an instance of the test class
                //
                var testInstance = Activator.CreateInstance(testClass.Type);

                //
                // Set the instance's TestContext property, if present
                //
                if (testClass.TestContextSetter != null)
                {
                    MethodRunner.Run(
                        testClass.TestContextSetter, testInstance,
                        true,
                        null, false,
                        null);
                }

                //
                // Invoke [TestInitialize], [TestMethod], and [TestCleanup]
                //
                bool testInitializeSucceeded = false;
                bool testMethodSucceeded     = false;
                bool testCleanupSucceeded    = false;

                TestContext.CurrentTestOutcome = UnitTestOutcome.InProgress;

                testInitializeSucceeded =
                    MethodRunner.Run(
                        testInitializeMethod, testInstance,
                        false,
                        null, false,
                        "[TestInitialize]");

                if (testInitializeSucceeded)
                {
                    testMethodSucceeded =
                        MethodRunner.Run(
                            testMethod.MethodInfo, testInstance,
                            false,
                            testMethod.ExpectedException, testMethod.AllowDerivedExpectedExceptionTypes,
                            "[TestMethod]");

                    TestContext.CurrentTestOutcome =
                        testMethodSucceeded
                            ? UnitTestOutcome.Passed
                            : UnitTestOutcome.Failed;

                    testCleanupSucceeded =
                        MethodRunner.Run(
                            testCleanupMethod, testInstance,
                            false,
                            null, false,
                            "[TestCleanup]");
                }

                bool passed = testInitializeSucceeded && testMethodSucceeded && testCleanupSucceeded;

                WriteLine();
                WriteLine(passed ? "Passed" : "FAILED");

                return(passed ? UnitTestOutcome.Passed : UnitTestOutcome.Failed);
            }
            finally
            {
                TestContext.TestName           = null;
                TestContext.CurrentTestOutcome = UnitTestOutcome.Unknown;
            }
        }
Ejemplo n.º 3
0
        static bool Run(TestAssembly testAssembly)
        {
            bool assemblyInitializeSucceeded = false;
            int  failed = 0;
            bool assemblyCleanupSucceeded = false;

            //
            // Use the test assembly's .config file if present
            //
            ConfigFileSwitcher.SwitchTo(testAssembly.Assembly.Location + ".config");

            //
            // Run [AssemblyInitialize] method
            //
            TestContext.FullyQualifiedTestClassName = testAssembly.TestClasses.First().FullName;
            TestContext.TestName           = testAssembly.TestClasses.First().TestMethods.First().Name;
            TestContext.CurrentTestOutcome = UnitTestOutcome.InProgress;

            assemblyInitializeSucceeded =
                MethodRunner.Run(
                    testAssembly.AssemblyInitializeMethod, null,
                    true,
                    null, false,
                    "[AssemblyInitialize]");

            TestContext.FullyQualifiedTestClassName = null;
            TestContext.TestName           = null;
            TestContext.CurrentTestOutcome = UnitTestOutcome.Unknown;

            if (assemblyInitializeSucceeded)
            {
                //
                // Run tests in each [TestClass]
                //
                if (assemblyInitializeSucceeded)
                {
                    foreach (var testClass in testAssembly.TestClasses)
                    {
                        if (!TestClassRunner.Run(testClass))
                        {
                            failed++;
                        }
                    }
                }

                //
                // Run [AssemblyCleanup] method
                //
                assemblyCleanupSucceeded =
                    MethodRunner.Run(
                        testAssembly.AssemblyCleanupMethod, null,
                        false,
                        null, false,
                        "[AssemblyCleanup]");
            }

            return
                (assemblyInitializeSucceeded &&
                 failed == 0 &&
                 assemblyCleanupSucceeded);
        }