Esempio n. 1
0
        internal UnitTestResult RunUnitTest(UnitTest test, string suiteName, string pathName, string testName, TestContext testContext)
        {
            var runnerExe = GetCustomConsoleRunnerCommand();

            if (runnerExe != null)
            {
                return(RunWithConsoleRunner(runnerExe, test, suiteName, pathName, testName, testContext));
            }

            ExternalTestRunner runner       = (ExternalTestRunner)Runtime.ProcessService.CreateExternalProcessObject(typeof(ExternalTestRunner), testContext.ExecutionContext, UserAssemblyPaths);
            LocalTestMonitor   localMonitor = new LocalTestMonitor(testContext, test, suiteName, testName != null);

            ITestFilter filter = null;

            if (test != null)
            {
                if (test is UnitTestGroup)
                {
                    filter = new TestNameFilter(CollectTests((UnitTestGroup)test));
                }
                else
                {
                    filter = new TestNameFilter(test.TestId);
                }
            }
            else
            {
                NUnitCategoryOptions categoryOptions = (NUnitCategoryOptions)test.GetOptions(typeof(NUnitCategoryOptions));
                if (categoryOptions.EnableFilter && categoryOptions.Categories.Count > 0)
                {
                    string[] cats = new string [categoryOptions.Categories.Count];
                    categoryOptions.Categories.CopyTo(cats, 0);
                    filter = new CategoryFilter(cats);
                    if (categoryOptions.Exclude)
                    {
                        filter = new NotFilter(filter);
                    }
                }
            }

            RunData rd = new RunData();

            rd.Runner       = runner;
            rd.Test         = this;
            rd.LocalMonitor = localMonitor;
            testContext.Monitor.CancelRequested += new TestHandler(rd.Cancel);

            UnitTestResult result;

            try {
                if (string.IsNullOrEmpty(AssemblyPath))
                {
                    string msg = GettextCatalog.GetString("Could not get a valid path to the assembly. There may be a conflict in the project configurations.");
                    throw new Exception(msg);
                }
                System.Runtime.Remoting.RemotingServices.Marshal(localMonitor, null, typeof(IRemoteEventListener));

                string testRunnerAssembly, testRunnerType;
                GetCustomTestRunner(out testRunnerAssembly, out testRunnerType);

                result = runner.Run(localMonitor, filter, AssemblyPath, "", new List <string> (SupportAssemblies), testRunnerType, testRunnerAssembly);
                if (testName != null)
                {
                    result = localMonitor.SingleTestResult;
                }
            } catch (Exception ex) {
                if (!localMonitor.Canceled)
                {
                    LoggingService.LogError(ex.ToString());
                    if (localMonitor.RunningTest != null)
                    {
                        RuntimeErrorCleanup(testContext, localMonitor.RunningTest, ex);
                    }
                    else
                    {
                        testContext.Monitor.ReportRuntimeError(null, ex);
                        throw;
                    }
                    result = UnitTestResult.CreateFailure(ex);
                }
                else
                {
                    result = UnitTestResult.CreateFailure(GettextCatalog.GetString("Canceled"), null);
                }
            } finally {
                testContext.Monitor.CancelRequested -= new TestHandler(rd.Cancel);
                runner.Dispose();
                System.Runtime.Remoting.RemotingServices.Disconnect(localMonitor);
            }

            return(result);
        }
        internal UnitTestResult RunUnitTest(UnitTest test, string suiteName, string pathName, string testName, TestContext testContext)
        {
            var runnerExe = GetCustomConsoleRunnerCommand();

            if (runnerExe != null)
            {
                return(RunWithConsoleRunner(runnerExe, test, suiteName, pathName, testName, testContext));
            }
            var console = IdeApp.Workbench?.ProgressMonitors.ConsoleFactory.CreateConsole();
            ExternalTestRunner runner       = (ExternalTestRunner)Runtime.ProcessService.CreateExternalProcessObject(typeof(ExternalTestRunner), testContext.ExecutionContext, UserAssemblyPaths, console);
            LocalTestMonitor   localMonitor = new LocalTestMonitor(testContext, test, suiteName, testName != null);

            ITestFilter filter = null;

            if (test != null)
            {
                if (test is UnitTestGroup)
                {
                    filter = new TestNameFilter(CollectTests((UnitTestGroup)test));
                }
                else
                {
                    filter = new TestNameFilter(test.TestId);
                }
            }

            RunData rd = new RunData();

            rd.Runner       = runner;
            rd.Test         = this;
            rd.LocalMonitor = localMonitor;
            testContext.Monitor.CancelRequested += new TestHandler(rd.Cancel);

            UnitTestResult result;
            var            crashLogFile = Path.GetTempFileName();

            try {
                if (string.IsNullOrEmpty(AssemblyPath))
                {
                    string msg = GettextCatalog.GetString("Could not get a valid path to the assembly. There may be a conflict in the project configurations.");
                    throw new Exception(msg);
                }
                System.Runtime.Remoting.RemotingServices.Marshal(localMonitor, null, typeof(IRemoteEventListener));

                string testRunnerAssembly, testRunnerType;
                GetCustomTestRunner(out testRunnerAssembly, out testRunnerType);

                result = runner.Run(localMonitor, filter, AssemblyPath, "", new List <string> (SupportAssemblies), testRunnerType, testRunnerAssembly, crashLogFile);
                if (testName != null)
                {
                    result = localMonitor.SingleTestResult;
                }

                ReportCrash(testContext, crashLogFile);
            } catch (Exception ex) {
                if (ReportCrash(testContext, crashLogFile))
                {
                    result = UnitTestResult.CreateFailure(GettextCatalog.GetString("Unhandled exception"), null);
                }
                else if (!localMonitor.Canceled)
                {
                    LoggingService.LogError(ex.ToString());
                    if (localMonitor.RunningTest != null)
                    {
                        RuntimeErrorCleanup(testContext, localMonitor.RunningTest, ex);
                    }
                    else
                    {
                        testContext.Monitor.ReportRuntimeError(null, ex);
                        throw;
                    }
                    result = UnitTestResult.CreateFailure(ex);
                }
                else
                {
                    result = UnitTestResult.CreateFailure(GettextCatalog.GetString("Canceled"), null);
                }
            } finally {
                File.Delete(crashLogFile);
                testContext.Monitor.CancelRequested -= new TestHandler(rd.Cancel);
                runner.Dispose();
                System.Runtime.Remoting.RemotingServices.Disconnect(localMonitor);
            }

            return(result);
        }