/// <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(); } } } }
/// <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(); } } } }