internal UnitTestResult RunUnitTest(UnitTest test, string suiteName, TestContext testContext)
        {
            ExternalTestRunner runner = (ExternalTestRunner) Runtime.ProcessService.CreateExternalProcessObject (typeof(ExternalTestRunner), false);
            LocalTestMonitor localMonitor = new LocalTestMonitor (testContext, runner, test, suiteName);

            IFilter filter = null;

            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, categoryOptions.Exclude);
            }

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

            UnitTestResult result;

            try {
                TestResult res = runner.Run (localMonitor, filter, AssemblyPath, suiteName, null);
                result = localMonitor.GetLocalTestResult (res);
            } catch (Exception ex) {
                Console.WriteLine (ex);
                if (localMonitor.RunningTest != null) {
                    RuntimeErrorCleanup (testContext, localMonitor.RunningTest, ex);
                } else {
                    testContext.Monitor.ReportRuntimeError (null, ex);
                    throw ex;
                }
                result = UnitTestResult.CreateFailure (ex);
            } finally {
                testContext.Monitor.CancelRequested -= new TestHandler (rd.Cancel);
                runner.Dispose ();
            }

            return result;
        }
Beispiel #2
0
        /// <summary>
        /// Runs the tests and sets up the formatters.
        /// </summary>
        protected override void ExecuteTask()
        {
            if (FormatterElements.Count == 0) {
                FormatterElement defaultFormatter = new FormatterElement();
                defaultFormatter.Project = Project;
                defaultFormatter.NamespaceManager = NamespaceManager;
                defaultFormatter.Type = FormatterType.Plain;
                defaultFormatter.UseFile = false;
                FormatterElements.Add(defaultFormatter);

                Log(Level.Warning, "No <formatter .../> element was specified." +
                    " A plain-text formatter was added to prevent losing output of the" +
                    " test results.");

                Log(Level.Warning, "Add a <formatter .../> element to the" +
                    " <nunit2> task to prevent this warning from being output and" +
                    " to ensure forward compatibility with future revisions of NAnt.");
            }

            LogWriter logWriter = new LogWriter(this, Level.Info, CultureInfo.InvariantCulture);
            EventListener listener = new EventCollector(logWriter, logWriter);

            foreach (NUnit2Test testElement in Tests) {
                IFilter categoryFilter = null;

                // include or exclude specific categories
                string categories = testElement.Categories.Includes.ToString();
                if (!StringUtils.IsNullOrEmpty(categories)) {
                    categoryFilter = new CategoryFilter(categories.Split(','), false);
                } else {
                    categories = testElement.Categories.Excludes.ToString();
                    if (!StringUtils.IsNullOrEmpty(categories)) {
                        categoryFilter = new CategoryFilter(categories.Split(','), true);
                    }
                }

                foreach (string testAssembly in testElement.TestAssemblies) {
                    NUnit2TestDomain domain = new NUnit2TestDomain();

                    try {
                        TestRunner runner = domain.CreateRunner(
                            new FileInfo(testAssembly),
                            testElement.AppConfigFile,
                            testElement.References.FileNames);

                        Test test = null;
                        if (testElement.TestName != null) {
                            test = runner.Load(testAssembly, testElement.TestName);
                        } else {
                            test = runner.Load(testAssembly);
                        }

                        if (test == null) {
                            Log(Level.Warning, "Assembly \"{0}\" contains no tests.",
                                testAssembly);
                            continue;
                        }

                        // set category filter
                        if (categoryFilter != null) {
                            runner.Filter = categoryFilter;
                        }

                        // run test
                        TestResult result = runner.Run(listener);

                        // flush test output to log
                        logWriter.Flush();

                        // format test results using specified formatters
                        FormatResult(testElement, result);

                        if (result.IsFailure && (testElement.HaltOnFailure || HaltOnFailure)) {
                            throw new BuildException("Tests Failed.", Location);
                        }
                    } catch (BuildException) {
                        // re-throw build exceptions
                        throw;
                    } catch (Exception ex) {
                        if (!FailOnError) {
                            // just log error and continue with next test
                            Log(Level.Error, LogPrefix + "NUnit Error: " + ex.ToString());
                            continue;
                        }

                        Version nunitVersion = typeof(TestResult).Assembly.GetName().Version;

                        throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                            "Failure executing test(s). If you assembly is not built using"
                            + " NUnit version {0}, then ensure you have redirected assembly"
                            + " bindings. Consult the documentation of the <nunit2> task"
                            + " for more information.", nunitVersion), Location, ex);
                    } finally {
                        domain.Unload();

                        // flush test output to log
                        logWriter.Flush();
                    }
                }
            }
        }