Ejemplo n.º 1
0
        static public bool Run(string assemblyPath)
        {
            Guard.NotNull(assemblyPath, "assemblyPath");

            WriteLine();
            WriteHeading("Assembly: " + assemblyPath);

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

            if (!File.Exists(fullAssemblyPath))
            {
                WriteLine();
                WriteLine("Test assembly not found: {0}", fullAssemblyPath);
                return(false);
            }

            //
            // Load test assembly
            //
            Assembly assembly;

            try
            {
                assembly = Assembly.LoadFrom(fullAssemblyPath);
            }
            catch (BadImageFormatException)
            {
                WriteLine();
                WriteLine("Not a .NET assembly: {0}", fullAssemblyPath);
                return(true);
            }
            var testAssembly = TestAssembly.TryCreate(assembly);

            if (testAssembly == null)
            {
                WriteLine();
                WriteLine("Not a test assembly: {0}", fullAssemblyPath);
                return(true);
            }
            WriteLine();
            WriteLine("Test Assembly:");
            WriteLine(assembly.Location);

            return(Run(testAssembly));
        }
        static bool RunTestAssembly(string assemblyPath)
        {
            Guard.NotNull(assemblyPath, "assemblyPath");

            Console.Out.WriteLine();
            WriteHeading("Assembly: " + assemblyPath);

            //
            // Resolve full path to test assembly
            //
            string fullAssemblyPath = GetFullAssemblyPath(assemblyPath);

            if (!File.Exists(fullAssemblyPath))
            {
                Console.Out.WriteLine();
                Console.Out.WriteLine("Test assembly not found: {0}", fullAssemblyPath);
                return(false);
            }

            //
            // Load test assembly
            //
            Assembly assembly;

            try
            {
                assembly = Assembly.LoadFrom(fullAssemblyPath);
            }
            catch (BadImageFormatException)
            {
                Console.Out.WriteLine();
                Console.Out.WriteLine("Not a .NET assembly: {0}", fullAssemblyPath);
                return(true);
            }
            var testAssembly = TestAssembly.TryCreate(assembly);

            if (testAssembly == null)
            {
                Console.Out.WriteLine();
                Console.Out.WriteLine("Not a test assembly: {0}", fullAssemblyPath);
                return(true);
            }
            Console.Out.WriteLine();
            Console.Out.WriteLine("Test Assembly:");
            Console.Out.WriteLine(assembly.Location);

            //
            // Use the test assembly's .config file if present
            //
            UseConfigFile(fullAssemblyPath);

            bool assemblyInitializeSucceeded = false;
            int  failed = 0;
            bool assemblyCleanupSucceeded = false;

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

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

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

            return
                (assemblyInitializeSucceeded &&
                 failed == 0 &&
                 assemblyCleanupSucceeded);
        }
        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());
        }