public List<TestCase> parse(Path surefirePath) throws IOException { SurefireReportParser parser = new SurefireReportParser(Arrays.asList(surefirePath.toFile()), Locale.ENGLISH); List<ReportTestSuite> suites; try { suites = parser.parseXMLReportFiles(); } catch (MavenReportException e) { throw new IOException("Failed to parse surefire reports", e); } return suites.stream() .flatMap(suite -> suite.getTestCases().stream()) .map(testCase -> new TestCase(testCase.getFullName(), testCase.getFailure() == null, parseTestFailure(testCase.getFailure()))) .collect(Collectors.toList()); }
private static List<Failure> parseReports(final File reportsDirectory, final Set<String> excludeSpecs) { SurefireReportParser parser = new SurefireReportParser(Collections.singletonList(reportsDirectory), Locale.getDefault()); String currentPackageName = ""; List<Failure> failures = new ArrayList<Failure>(); try { List<ReportTestSuite> parsedReports = parser.parseXMLReportFiles(); for (ReportTestSuite parsedReport : parsedReports) { String packageName = parsedReport.getPackageName(); if (packageName.length() > 0) { currentPackageName = packageName; } if (parsedReport.getNumberOfErrors() > 0) { for (ReportTestCase reportTestCase : parsedReport.getTestCases()) { String name = parsedReport.getFullClassName(); boolean ignored = excludeSpecs.contains(getSpecIdentifier(currentPackageName, name)); String failureDetail = reportTestCase.getFailureDetail(); String[] failureTokens = failureDetail.split("\n"); String expected = failureTokens.length > 0 ? failureTokens[0] : ""; String actual = failureTokens.length > 1 ? failureTokens[1] : ""; failures.add(new Failure(name, currentPackageName, actual, expected, ignored)); } } } } catch (MavenReportException e) { e.printStackTrace(); } return failures; }
public void run() throws MojoExecutionException { try { Xpp3Dom domNode = this.applyNonDexConfig((Xpp3Dom) this.surefire.getConfiguration()); this.setupArgline(domNode); Logger.getGlobal().log(Level.FINE, "Config node passed: " + domNode.toString()); Logger.getGlobal().log(Level.FINE, this.mavenProject + "\n" + this.mavenSession + "\n" + this.pluginManager); Logger.getGlobal().log(Level.CONFIG, this.configuration.toString()); Logger.getGlobal().log(Level.FINE, "Surefire config: " + this.surefire + " " + MojoExecutor.goal("test") + " " + domNode + " " + MojoExecutor.executionEnvironment(this.mavenProject, this.mavenSession, this.pluginManager)); MojoExecutor.executeMojo(this.surefire, MojoExecutor.goal("test"), domNode, MojoExecutor.executionEnvironment(this.mavenProject, this.mavenSession, this.pluginManager)); } catch (MojoExecutionException mojoException) { Logger.getGlobal().log(Level.INFO, "Surefire failed when running tests for " + this.configuration.executionId); SurefireReportParser parser = new SurefireReportParser( Arrays.asList(this.configuration.getExecutionDir().toFile()), Locale.getDefault()); try { Set<String> failingTests = new LinkedHashSet<>(); for (ReportTestSuite report : parser.parseXMLReportFiles()) { for (ReportTestCase testCase : report.getTestCases()) { // Record if failed, but not skipped if (testCase.hasFailure() && !"skipped".equals(testCase.getFailureType())) { failingTests.add(testCase.getFullClassName() + '#' + testCase.getName()); } } } this.configuration.setFailures(failingTests); } catch (MavenReportException ex) { throw new MojoExecutionException("Failed to parse mvn reports!"); } throw mojoException; } catch (Throwable tr) { Logger.getGlobal().log(Level.SEVERE, "Some exception that is highly unexpected: ", tr); throw tr; } }
/** * Notifies the watcher that a new file is created. It selects and executes the test. Failures and errors are * reported in the thrown {@link org.wisdom.maven.WatchingException}. * * @param file is the file. * @return return {@code true} * @throws org.wisdom.maven.WatchingException if the test execution failed. */ @Override public boolean fileCreated(File file) throws WatchingException { // Check selection policy String testParameter = null; if (testSelectionPolicy == TestSelectionPolicy.SELECTIVE) { // The test selection is done using the -Dtest parameter from surefire // We should also take care that the changed file is not a 'test', in that case, we run only this one. final String filename = file.getName().substring(0, file.getName().lastIndexOf(".")); if (filename.startsWith("Test") || filename.endsWith("Test") || filename.endsWith("TestCase")) { testParameter = filename; } else { // It's a business class // Be aware of #365, the selection must select only unit tests, so the expression should be // TestFileName*,*FileNameTest* // The *FileNameTestCase case can be ignored (included by the second expression) testParameter = "Test" + filename + "*,*" + filename + "Test*"; } } try { execute(testParameter); return true; } catch (MojoExecutionException e) { getLog().debug("An error occurred while executing Surefire", e); // Compute the Watching Exception content. StringBuilder message = new StringBuilder(); SurefireReportParser parser = new SurefireReportParser(ImmutableList.of(reports), Locale.ENGLISH); try { computeTestFailureMessageFromReports(message, parser); throw new WatchingException("Unit Test Failures", message.toString(), file, e); } catch (MavenReportException reportException) { // Cannot read the reports. throw new WatchingException("Unit Test Failures", file, reportException); }} }
private static void computeTestFailureMessageFromReports(StringBuilder message, SurefireReportParser parser) throws MavenReportException { List<ReportTestSuite> suites = parser.parseXMLReportFiles(); Map<String, String> summary = parser.getSummary(suites); message .append(summary.get("totalTests")) .append(" tests, ") .append(summary.get("totalErrors")) .append(" errors, ") .append(summary.get("totalFailures")) .append(" failures, ") .append(summary.get("totalSkipped")) .append(" skipped ") .append("(executed in ") .append(summary.get("totalElapsedTime")) .append("s)<br/><ul>"); for (ReportTestSuite suite : suites) { if (suite.getNumberOfErrors() > 0 || suite.getNumberOfFailures() > 0) { for (ReportTestCase tc : suite.getTestCases()) { if (tc.getFailure() != null && !"skipped".equalsIgnoreCase((String) tc.getFailure().get("message"))) { message .append("<li><em>") .append(tc.getFullName()) .append("</em> failed: ") .append(tc.getFailure().get("message")) .append("</li>"); } } } } message.append("</ul>"); }