Skip to content

Commit

Permalink
Adjust to changed Surefire-API / changed xml output
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jan 29, 2023
1 parent 9f263bc commit 95199fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ public void testJUnit5Runner() throws Exception {
verifier.executeGoal("verify");
verifier.verifyErrorFreeLog();
String projectBasedir = verifier.getBasedir();
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit4Test", "testWithJUnit4");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test", "My 1st JUnit 5 test!");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test", "[1] one");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test", "[2] two");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test", "repetition 1 of 3");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test", "repetition 2 of 3");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test", "repetition 3 of 3");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test",
"parameterizedJUnit5Test(String)[1] one");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test",
"parameterizedJUnit5Test(String)[2] two");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test",
"repeatedJUnit5Test() repetition 1 of 3");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test",
"repeatedJUnit5Test() repetition 2 of 3");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit5Test",
"repeatedJUnit5Test() repetition 3 of 3");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit4Test", "testWithJUnit4");
// make sure test tagged as 'slow' was skipped
assertNumberOfSuccessfulTests(projectBasedir, "bundle.test.JUnit5Test", 6);
}
Expand Down Expand Up @@ -85,9 +90,12 @@ public void testJUnit59Runner() throws Exception {
verifier.verifyErrorFreeLog();
String projectBasedir = verifier.getBasedir();
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test", "My 1st JUnit 5.9 test!");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test", "[1] 0, 5, 5");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test", "[2] 10, 10, 20");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test", "[3] 12, 30, 42");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test",
"parameterizedJUnit59TestWithMethodSource(int, int, int)[1] 0, 5, 5");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test",
"parameterizedJUnit59TestWithMethodSource(int, int, int)[2] 10, 10, 20");
assertTestMethodWasSuccessfullyExecuted(projectBasedir, "bundle.test.JUnit59Test",
"parameterizedJUnit59TestWithMethodSource(int, int, int)[3] 12, 30, 42");
// make sure test tagged as 'slow' was skipped
assertNumberOfSuccessfulTests(projectBasedir, "bundle.test.JUnit59Test", 4);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public static File testResultFile(String baseDir, String testSuffix) {

public static void assertTestMethodWasSuccessfullyExecuted(String baseDir, String className, String methodName,
int iterations) throws Exception {
Document document = readDocument(baseDir, className);
File resultFile = getTestResultFile(baseDir, className);
Document document = readDocument(resultFile);
XPath xpath = XPathFactory.newInstance().newXPath();
// surefire-test-report XML schema:
// https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd
String testCaseXPath = String.format("/testsuite/testcase[@classname='%s' and @name='%s']", className,
methodName);
NodeList testCaseNodes = (NodeList) xpath.evaluate(testCaseXPath, document, XPathConstants.NODESET);
assertEquals(iterations, testCaseNodes.getLength());
assertEquals(resultFile.getAbsolutePath() + " with xpath " + testCaseXPath
+ " does not match the number of iterations", iterations, testCaseNodes.getLength());

NodeList failureNodes = (NodeList) xpath.evaluate(testCaseXPath + "/failure", document, XPathConstants.NODESET);
assertEquals(0, failureNodes.getLength());
Expand Down Expand Up @@ -93,11 +95,19 @@ private static int extractNumericAttribute(String baseDir, String className, Str
}

private static Document readDocument(String baseDir, String className) throws Exception {
File sureFireTestReport = new File(baseDir, "target/surefire-reports/TEST-" + className + ".xml");
return readDocument(getTestResultFile(baseDir, className));
}

private static Document readDocument(File sureFireTestReport) throws Exception {
assertTrue(sureFireTestReport.isFile());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(sureFireTestReport);
return document;
}

private static File getTestResultFile(String baseDir, String className) {
File sureFireTestReport = new File(baseDir, "target/surefire-reports/TEST-" + className + ".xml");
return sureFireTestReport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public class OsgiSurefireBooter {
private static final String JUNIT_PLATFORM_PROVIDER = "org.apache.maven.surefire.junitplatform.JUnitPlatformProvider";

public static int run(String[] args, Properties testProps) throws Exception {
boolean failIfNoTests = Boolean.parseBoolean(testProps.getProperty("failifnotests", "false"));
// TODO eventually make use of parameter redirectTestOutputToFile
@SuppressWarnings("unused")
boolean redirectTestOutputToFile = Boolean
Expand Down Expand Up @@ -122,12 +121,12 @@ public static int run(String[] args, Properties testProps) throws Exception {
// TODO dir scanning with no includes done here (done in TestMojo already)
// but without dirScannerParams we get an NPE accessing runOrder
DirectoryScannerParameters dirScannerParams = new DirectoryScannerParameters(testClassesDir,
Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), failIfNoTests, runOrder);
Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), runOrder);
ReporterConfiguration reporterConfig = new ReporterConfiguration(reportsDir, trimStackTrace);
TestRequest testRequest = new TestRequest(suiteXmlFiles, testClassesDir,
TestListResolver.getEmptyTestListResolver(), rerunFailingTestsCount);
ProviderConfiguration providerConfiguration = new ProviderConfiguration(dirScannerParams,
new RunOrderParameters(runOrder, null), failIfNoTests, reporterConfig, null, testRequest,
new RunOrderParameters(runOrder, null), reporterConfig, null, testRequest,
extractProviderProperties(testProps), null, false, Collections.emptyList(), skipAfterFailureCount,
Shutdown.DEFAULT, 30);
StartupReportConfiguration startupReportConfig = new StartupReportConfiguration(useFile, printSummary,
Expand Down

0 comments on commit 95199fa

Please sign in to comment.