Implements a TextWriter for writing information to the NAnt logging infrastructure.
상속: System.IO.TextWriter
예제 #1
0
        protected override void ExecuteTask()
        {
            if (TestAssemblies.FileNames.Count == 0)
                throw new BuildException("At least one test assembly is required");

            var nantLogWriter = new LogWriter(this, Level.Info, CultureInfo.InvariantCulture);
            var output = new PlainTextOutput(nantLogWriter);

            WriteHeaderInto(output);

            var listener = EventListeners.CreateEventListenerUsing(nantLogWriter,
                                                                              TextOutputFile,
                                                                              XmlOutputFile);

            var runner = NBehaveConfiguration
                .New
                .SetDryRun(DryRun)
                .SetScenarioFiles(ScenarioFiles.FileNames.Cast<string>().ToList())
                .SetAssemblies(TestAssemblies.FileNames.Cast<string>().ToList())
                .SetEventListener(listener)
                .Build();

            var results = runner.Run();

            if (DryRun)
                return;

            if (FailBuild)
                FailBuildBasedOn(results);
        }
예제 #2
0
        private void FormatResult(NUnit2Test testElement, TestResult result)
        {
            // temp file for storing test results
            string xmlResultFile = Path.GetTempFileName();

            try {
                XmlResultWriter resultWriter = new XmlResultWriter(xmlResultFile);
                resultWriter.SaveTestResult(result);

                foreach (FormatterElement formatter in FormatterElements) {
                    // permanent file for storing test results
                    string outputFile = result.Name + "-results" + formatter.Extension;

                    if (formatter.Type == FormatterType.Xml) {
                        if (formatter.UseFile) {

                            if (formatter.OutputDirectory != null) {
                                // ensure output directory exists
                                if (!formatter.OutputDirectory.Exists) {
                                    formatter.OutputDirectory.Create();
                                }

                                // combine output directory and result filename
                                outputFile = Path.Combine(formatter.OutputDirectory.FullName,
                                    Path.GetFileName(outputFile));
                            }

                            // copy the temp result file to permanent location
                            File.Copy(xmlResultFile, outputFile, true);
                        } else {
                            using (StreamReader reader = new StreamReader(xmlResultFile)) {
                                // strip off the xml header
                                reader.ReadLine();
                                StringBuilder builder = new StringBuilder();
                                while (reader.Peek() > -1) {
                                    builder.Append(reader.ReadLine().Trim()).Append(
                                        Environment.NewLine);
                                }
                                Log(Level.Info, builder.ToString());
                            }
                        }
                    } else if (formatter.Type == FormatterType.Plain) {
                        TextWriter writer;
                        if (formatter.UseFile) {

                            if (formatter.OutputDirectory != null) {
                                // ensure output directory exists
                                if (!formatter.OutputDirectory.Exists) {
                                    formatter.OutputDirectory.Create();
                                }

                                // combine output directory and result filename
                                outputFile = Path.Combine(formatter.OutputDirectory.FullName,
                                    Path.GetFileName(outputFile));
                            }

                            writer = new StreamWriter(outputFile);
                        } else {
                            writer = new LogWriter(this, Level.Info, CultureInfo.InvariantCulture);
                        }
                        CreateSummaryDocument(xmlResultFile, writer, testElement);
                        writer.Close();
                    }
                }
            } catch (Exception ex) {
                throw new BuildException("Test results could not be formatted.",
                    Location, ex);
            } finally {
                // make sure temp file with test results is removed
                File.Delete(xmlResultFile);
            }
        }
예제 #3
0
 /// <summary>
 /// Gets a new EventListener to use for the unit tests.
 /// </summary>
 /// <returns>
 /// A new EventListener created with a new EventCollector that
 /// is initialized with <paramref name="logWriter"/>.
 /// </returns>
 /// <param name='logWriter'>
 /// Log writer to send test output to.
 /// </param>
 protected virtual EventListener GetListener(LogWriter logWriter)
 {
     return new EventCollector(logWriter, logWriter, _labels);
 }
예제 #4
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 = GetListener(logWriter);

            foreach (NUnit2Test testElement in Tests) {
                // Setup the test filter var to setup include/exclude filters.
                ITestFilter testFilter = null;

                // If the include categories contains values, setup the category
                // filter with the include categories.
                string includes = testElement.Categories.Includes.ToString();
                if (!String.IsNullOrEmpty(includes))
                {
                    testFilter = new CategoryFilter(includes.Split(','));
                }
                else
                {
                    // If the include categories does not have includes but
                    // contains excludes, setup the category filter with the excludes
                    // and use the Not filter to tag the categories for exclude.
                    string excludes = testElement.Categories.Excludes.ToString();
                    if (!String.IsNullOrEmpty(excludes))
                    {
                        ITestFilter excludeFilter = new CategoryFilter(excludes.Split(','));
                        testFilter = new NotFilter(excludeFilter);
                    }
                    else
                    {
                        // If the categories do not contain includes or excludes,
                        // assign the testFilter var with an empty filter.
                        testFilter = TestFilter.Empty;
                    }
                }

                foreach (string testAssembly in testElement.TestAssemblies) {
                    // Setup the NUnit2TestDomain var.
                    NUnit2TestDomain domain = new NUnit2TestDomain();
                    // Setup the TestPackage var to use with the testrunner var
                    TestPackage package = new TestPackage(testAssembly);

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

                        // If the name of the current test element is not null,
                        // use it for the package test name.
                        if (!String.IsNullOrEmpty(testElement.TestName))
                        {
                            package.TestName = testElement.TestName;
                        }

                        // Bool var containing the result of loading the test package
                        // in the TestRunner var.
                        bool successfulLoad = runner.Load(package);

                        // If the test package load was successful, proceed with the
                        // testing.
                        if (successfulLoad)
                        {
                            // If the runner does not contain any tests, proceed
                            // to the next assembly.
                            if (runner.Test == null) {
                                Log(Level.Warning, "Assembly \"{0}\" contains no tests.",
                                    testAssembly);
                                continue;
                            }

                            // Setup and run tests
                            TestResult result = runner.Run(listener, testFilter,
                                    true, GetLoggingThreshold());

                            // 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);
                            }
                        }
                        else
                        {
                            // If the package load failed, throw a build exception.
                            throw new BuildException("Test Package Load Failed.", Location);
                        }
                    } catch (BuildException) {
                        // re-throw build exceptions
                        throw;
                    } catch (Exception ex) {
                        if (!FailOnError) {
                            // just log error and continue with next test

                            // TODO: (RMB) Need to make sure that this is the correct way to proceed with displaying NUnit errors.
                            string logMessage =
                                String.Concat("[", Name, "] NUnit Error: ", ex.ToString());

                            Log(Level.Error, logMessage.PadLeft(Project.IndentationSize));
                            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();
                    }
                }
            }
        }
예제 #5
0
파일: NUnit2Task.cs 프로젝트: skolima/NAnt
        /// <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();
                    }
                }
            }
        }