Inheritance: NAnt.Core.Element
Beispiel #1
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();
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Adds a <see cref="FormatterElement"/> to the end of the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> to be added to the end of the collection.</param>
 /// <returns>The position into which the new element was inserted.</returns>
 public int Add(FormatterElement item)
 {
     return(base.List.Add(item));
 }
Beispiel #3
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();
                    }
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Inserts a <see cref="FormatterElement"/> into the collection at the specified index.
 /// </summary>
 /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
 /// <param name="item">The <see cref="FormatterElement"/> to insert.</param>
 public void Insert(int index, FormatterElement item)
 {
     base.List.Insert(index, item);
 }
Beispiel #5
0
 /// <summary>
 /// Removes a member from the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> to remove from the collection.</param>
 public void Remove(FormatterElement item)
 {
     base.List.Remove(item);
 }
Beispiel #6
0
 /// <summary>
 /// Determines whether a <see cref="FormatterElement"/> is in the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> to locate in the collection.</param>
 /// <returns>
 /// <see langword="true" /> if <paramref name="item"/> is found in the
 /// collection; otherwise, <see langword="false" />.
 /// </returns>
 public bool Contains(FormatterElement item)
 {
     return(base.List.Contains(item));
 }
Beispiel #7
0
 /// <summary>
 /// Retrieves the index of a specified <see cref="FormatterElement"/> object in the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> object for which the index is returned.</param>
 /// <returns>
 /// The index of the specified <see cref="FormatterElement"/>. If the <see cref="FormatterElement"/> is not currently a member of the collection, it returns -1.
 /// </returns>
 public int IndexOf(FormatterElement item)
 {
     return(base.List.IndexOf(item));
 }
 /// <summary>
 /// Adds the elements of a <see cref="FormatterElement"/> array to the end of the collection.
 /// </summary>
 /// <param name="items">The array of <see cref="FormatterElement"/> elements to be added to the end of the collection.</param> 
 public void AddRange(FormatterElement[] items) {
     for (int i = 0; (i < items.Length); i = (i + 1)) {
         Add(items[i]);
     }
 }
        private bool TestWithNUnit(string outputFilePath)
        {
            bool hasErrors = false;
            #region <nunit2>
            var task = new NUnit2Task();
            // this little assignment makes the whole TestTask very difficult to unit test
            // unless maybe we subclass Project for testing?
            task.Project = Project;

            #region <formatter type="Plain" />
            var formatter = new FormatterElement();
            formatter.Type = FormatterType.Plain;
            task.FormatterElements.Add(formatter);
            #endregion

            #region <test assemblyname="outputFilePath" />
            var test = new NUnit2Test();
            test.AssemblyFile = new FileInfo(outputFilePath);
            task.Tests.Add(test);
            #endregion

            try
            {
                task.Execute();
            }
            catch (BuildException be)
            {
                hasErrors = true;
                Log(Level.Error, be.Message);
            }
            #endregion
            return hasErrors;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FormatterElementCollection"/> class
 /// with the specified array of <see cref="FormatterElement"/> instances.
 /// </summary>
 public FormatterElementCollection(FormatterElement[] value) {
     AddRange(value);
 }
 /// <summary>
 /// Adds a <see cref="FormatterElement"/> to the end of the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> to be added to the end of the collection.</param> 
 /// <returns>The position into which the new element was inserted.</returns>
 public int Add(FormatterElement item) {
     return base.List.Add(item);
 }
 /// <summary>
 /// Removes a member from the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> to remove from the collection.</param>
 public void Remove(FormatterElement item) {
     base.List.Remove(item);
 }
 /// <summary>
 /// Inserts a <see cref="FormatterElement"/> into the collection at the specified index.
 /// </summary>
 /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
 /// <param name="item">The <see cref="FormatterElement"/> to insert.</param>
 public void Insert(int index, FormatterElement item) {
     base.List.Insert(index, item);
 }
 /// <summary>
 /// Retrieves the index of a specified <see cref="FormatterElement"/> object in the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> object for which the index is returned.</param> 
 /// <returns>
 /// The index of the specified <see cref="FormatterElement"/>. If the <see cref="FormatterElement"/> is not currently a member of the collection, it returns -1.
 /// </returns>
 public int IndexOf(FormatterElement item) {
     return base.List.IndexOf(item);
 }
 /// <summary>
 /// Copies the entire collection to a compatible one-dimensional array, starting at the specified index of the target array.        
 /// </summary>
 /// <param name="array">The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.</param> 
 /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
 public void CopyTo(FormatterElement[] array, int index) {
     base.List.CopyTo(array, index);
 }
 /// <summary>
 /// Determines whether a <see cref="FormatterElement"/> is in the collection.
 /// </summary>
 /// <param name="item">The <see cref="FormatterElement"/> to locate in the collection.</param> 
 /// <returns>
 /// <see langword="true" /> if <paramref name="item"/> is found in the 
 /// collection; otherwise, <see langword="false" />.
 /// </returns>
 public bool Contains(FormatterElement item) {
     return base.List.Contains(item);
 }