Ejemplo n.º 1
0
        /// <summary>
        /// Run tests in a [TestClass]
        /// </summary>
        ///
        static public void Run(TestClass testClass)
        {
            Guard.NotNull(testClass, nameof(testClass));

            EventHandlerPipeline.Raise(new TestClassBeginEvent()
            {
                FullName = testClass.FullName
            });

            do
            {
                //
                // Handle exclusion from the command line
                //
                if (!ArgumentParser.ClassShouldRun(testClass.FullName))
                {
                    EventHandlerPipeline.Raise(new TestClassIgnoredEvent()
                    {
                        IgnoredFromCommandLine = true
                    });
                    break;
                }

                //
                // Handle [Ignored] [TestClass]
                //
                if (testClass.IsIgnored)
                {
                    EventHandlerPipeline.Raise(new TestClassIgnoredEvent());
                    break;
                }

                //
                // Run [ClassInitialize] method
                //
                if (!MethodRunner.RunClassInitializeMethod(testClass))
                {
                    break;
                }

                //
                // Run [TestMethod]s
                //
                foreach (var testMethod in testClass.TestMethods)
                {
                    TestMethodRunner.Run(testMethod);
                }

                //
                // Run [ClassCleanup] method
                //
                MethodRunner.RunClassCleanupMethod(testClass);
            }while (false);

            EventHandlerPipeline.Raise(new TestClassEndEvent());
        }
        /// <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>
        ///
        static public void Run(TestMethod testMethod)
        {
            EventHandlerPipeline.Raise(new TestBeginEvent()
            {
                Name = testMethod.Name
            });

            do
            {
                //
                // Handle [Ignored] [TestMethod]
                //
                if (testMethod.IsIgnored)
                {
                    EventHandlerPipeline.Raise(new TestIgnoredEvent());
                    break;
                }

                //
                // Create instance of [TestClass]
                //
                var instance = Activator.CreateInstance(testMethod.TestClass.Type);

                //
                // Set TestContext property (if present)
                //
                MethodRunner.RunTestContextSetter(testMethod.TestClass, instance);

                //
                // Run [TestInitialize] method
                //
                if (!MethodRunner.RunTestInitializeMethod(testMethod.TestClass, instance))
                {
                    break;
                }

                //
                // Run [TestMethod]
                //
                MethodRunner.RunTestMethod(testMethod, instance);

                //
                // Run [TestCleanup] method
                //
                MethodRunner.RunTestCleanupMethod(testMethod.TestClass, instance);
            }while (false);

            EventHandlerPipeline.Raise(new TestEndEvent());
        }
        public static void Run(string assemblyPath)
        {
            Guard.NotNull(assemblyPath, nameof(assemblyPath));

            EventHandlerPipeline.Raise(new TestAssemblyBeginEvent()
            {
                Path = assemblyPath
            });

            do
            {
                //
                // Resolve full path to test assembly file
                //
                string fullAssemblyPath =
                    Path.IsPathRooted(assemblyPath)
                        ? assemblyPath
                        : Path.Combine(Environment.CurrentDirectory, assemblyPath);

                if (!File.Exists(fullAssemblyPath))
                {
                    EventHandlerPipeline.Raise(new TestAssemblyNotFoundEvent()
                    {
                        Path = fullAssemblyPath
                    });
                    break;
                }

                //
                // Load assembly
                //
                Assembly assembly;
                try
                {
                    assembly = Assembly.LoadFrom(fullAssemblyPath);
                }
                catch (BadImageFormatException)
                {
                    EventHandlerPipeline.Raise(new TestAssemblyNotDotNetEvent()
                    {
                        Path = fullAssemblyPath
                    });
                    break;
                }

                //
                // Interpret as test assembly
                //
                var testAssembly = TestAssembly.TryCreate(assembly);
                if (testAssembly == null)
                {
                    EventHandlerPipeline.Raise(new TestAssemblyNotTestEvent()
                    {
                        Path = fullAssemblyPath
                    });
                    break;
                }

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

                //
                // Run [AssemblyInitialize] method
                //
                if (!MethodRunner.RunAssemblyInitializeMethod(testAssembly))
                {
                    break;
                }

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

                //
                // Run [AssemblyCleanup] method
                //
                MethodRunner.RunAssemblyCleanupMethod(testAssembly);
            }while (false);

            EventHandlerPipeline.Raise(new TestAssemblyEndEvent());
        }