Skip to content

Commit

Permalink
Add a dummy test case in JUnit report if no features are found
Browse files Browse the repository at this point in the history
Jenkins will mark a job as failed even though the exit code was 0, if
the JUnit report file contains no testcase elements. Therefore add a
dummy testcase element to the JUnit report, when no features are found,
or no found features match the tags filter used.
  • Loading branch information
brasmusson committed Aug 2, 2013
1 parent 365d391 commit 0fcca0a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/cucumber/runtime/formatter/JUnitFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void done() {
try {
//set up a transformer
rootElement.setAttribute("failures", String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
if (rootElement.getElementsByTagName("testcase").getLength() == 0) {
addDummyTestCase(); // to avoid failed Jenkins jobs
}
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
Expand All @@ -108,6 +111,16 @@ public void done() {
}
}

private void addDummyTestCase() {
Element dummy = doc.createElement("testcase");
dummy.setAttribute("classname", "dummy");
dummy.setAttribute("name", "dummy");
rootElement.appendChild(dummy);
Element skipped = doc.createElement("skipped");
skipped.setAttribute("message", "No features found");
dummy.appendChild(skipped);
}

@Override
public void result(Result result) {
testCase.results.add(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,24 @@ public void should_handle_one_step_at_the_time_execution() throws Exception {
assertXmlEqual(expected, replaceTimeWithZeroTime(actual));
}

@Test
public void should_add_dummy_testcase_if_no_features_are_found_to_aviod_failed_jenkins_jobs() throws Exception {
final File report = File.createTempFile("cucumber-jvm-junit", ".xml");
final JUnitFormatter junitFormatter = createJUnitFormatter(report);

junitFormatter.done();
junitFormatter.close();

String actual = new Scanner(new FileInputStream(report), "UTF-8").useDelimiter("\\A").next();
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
"<testsuite failures=\"0\">\n" +
" <testcase classname=\"dummy\" name=\"dummy\">\n" +
" <skipped message=\"No features found\" />\n" +
" </testcase>\n" +
"</testsuite>\n";
assertXmlEqual(expected, replaceTimeWithZeroTime(actual));
}

private File runFeaturesWithJunitFormatter(final List<String> featurePaths) throws IOException {
return runFeaturesWithJunitFormatter(featurePaths, false);
}
Expand Down

0 comments on commit 0fcca0a

Please sign in to comment.