JUnit4ReportWriter implements ReportWriter { @Override public void writeReport(ReportTreeAccessor reportTreeAccessor, String executionId, File outputFile) throws IOException { ReportTreeVisitor visitor = new ReportTreeVisitor(reportTreeAccessor); try(BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { AtomicInteger numberOfTests = new AtomicInteger(0); AtomicInteger numberOfFailures = new AtomicInteger(0); AtomicInteger numberOfErrors = new AtomicInteger(0); AtomicInteger numberOfSkipped = new AtomicInteger(0); AtomicLong duration = new AtomicLong(); StringBuilder name = new StringBuilder(); visitor.visit(executionId, e->{ if(e.getStack().size()==0) { name.append(e.getNode().getName()); duration.set(e.getNode().getDuration()); } if(e.getStack().size()==1) { numberOfTests.incrementAndGet(); ReportNode node = e.getNode(); if(node.getStatus() == ReportNodeStatus.FAILED) { numberOfFailures.incrementAndGet(); } else if(node.getStatus() == ReportNodeStatus.TECHNICAL_ERROR) { numberOfErrors.incrementAndGet(); } else if(node.getStatus() == ReportNodeStatus.SKIPPED) { numberOfSkipped.incrementAndGet(); } } }); writer.write("<testsuite name=\""+name.toString()+"\" time=\""+formatTime(duration.get())+"\" tests=\""+numberOfTests.get()+"\" skipped=\""+numberOfSkipped.get()+"\" failures=\""+numberOfFailures.get()+"\" errors=\""+numberOfErrors.get()+"\">"); writer.newLine(); AtomicBoolean errorWritten = new AtomicBoolean(false); visitor.visit(executionId, new ReportNodeVisitorEventHandler() { @Override public void startReportNode(ReportNodeEvent event) { ReportNode node = event.getNode(); try { if(event.getStack().size()==1) { if(!skipReportNode(node)) { writer.write("<testcase classname=\""+node.getClass().getName()+"\" name=\""+node.getName()+"\" time=\""+formatTime(node.getDuration())+"\">"); errorWritten.set(false); } } else if (event.getStack().size()>1) { if(node.getError() != null) { writeErrorOrFailure(writer, node, errorWritten); } } } catch (IOException e1) { throw new RuntimeException(e1); } } @Override public void endReportNode(ReportNodeEvent event) { if(event.getStack().size()==1) { ReportNode node = event.getNode(); if(!skipReportNode(node)) { try { if(node.getStatus()!=ReportNodeStatus.PASSED && !errorWritten.get()) { writeErrorOrFailure(writer, node, errorWritten); } writer.write("</testcase>"); writer.newLine(); } catch (IOException e1) { throw new RuntimeException(e1); } } } } protected boolean skipReportNode(ReportNode node) { return node.getStatus()==ReportNodeStatus.SKIPPED || node.getStatus()==ReportNodeStatus.NORUN; } }); writer.write("</testsuite>"); writer.newLine(); writer.flush(); } }  @Override void writeReport(ReportTreeAccessor reportTreeAccessor, String executionId, File outputFile);  }
@Test public void testTestset() throws IOException { Plan plan = PlanBuilder.create() .startBlock(testSet("TS01")) .startBlock(testCase("TC01 - PASSED")) .add(checkArtefact(ReportNodeStatus.PASSED)) .endBlock() .startBlock(testCase("TC02 - PASSED in Sequence")) .startBlock(sequence()) .add(checkArtefact(ReportNodeStatus.PASSED)) .add(checkArtefact(ReportNodeStatus.PASSED)) .endBlock() .endBlock() .startBlock(testCase("TC03 - FAILED without error message")) .add(checkArtefact(ReportNodeStatus.PASSED)) .add(checkArtefact(ReportNodeStatus.FAILED)) .endBlock() .startBlock(testCase("TC04 - FAILED with error message")) .startBlock(for_()) .add(checkArtefact(ReportNodeStatus.FAILED, "my message")) .endBlock() .endBlock() .startBlock(testCase("TC05 - TECH_ERROR")) .add(checkArtefact(ReportNodeStatus.TECHNICAL_ERROR)) .endBlock() .startBlock(testCase("TC06 - TECH_ERROR with message")) .add(checkArtefact(ReportNodeStatus.TECHNICAL_ERROR, "My error message")) .endBlock() .startBlock(testCase("TC07 - SKIPPED")) .add(checkArtefact(ReportNodeStatus.SKIPPED)) .endBlock() .startBlock(testCase("TC08 - NORUN")) .add(checkArtefact(ReportNodeStatus.NORUN)) .endBlock() .endBlock().build(); File report = new File("TEST-JUnit4ReportWriterTest-testTestset.xml"); report.deleteOnExit(); ExecutionEngine engine = ExecutionEngine.builder().withPlugin(new ThreadPoolPlugin()).withPlugin(new BaseArtefactPlugin()).build(); engine.execute(plan).writeReport(new JUnit4ReportWriter(), report); PlanRunnerResultAssert.assertEquals(this.getClass(), "TEST-JUnit4ReportWriterTest-testTestset-expected.xml", report, "time=\".+?\""); }
@Test public void testSimpleSequence() throws IOException { Plan plan = PlanBuilder.create() .startBlock(sequence()) .add(checkArtefact(ReportNodeStatus.PASSED)) .startBlock(sequence()) .add(checkArtefact(ReportNodeStatus.PASSED)) .add(checkArtefact(ReportNodeStatus.PASSED)) .endBlock() .add(checkArtefact(ReportNodeStatus.FAILED)) .add(checkArtefact(ReportNodeStatus.FAILED, "my message")) .add(checkArtefact(ReportNodeStatus.TECHNICAL_ERROR)) .startBlock(testCase("TC - TECH_ERROR with message")) .add(checkArtefact(ReportNodeStatus.TECHNICAL_ERROR, "My error message")) .endBlock() .add(checkArtefact(ReportNodeStatus.SKIPPED)) .add(checkArtefact(ReportNodeStatus.NORUN)) .add(checkArtefact(ReportNodeStatus.INTERRUPTED)) .endBlock().build(); File report = new File("TEST-JUnit4ReportWriterTest-testSimpleSequence.xml"); report.deleteOnExit(); ExecutionEngine engine = ExecutionEngine.builder().withPlugin(new BaseArtefactPlugin()).build(); engine.execute(plan).writeReport(new JUnit4ReportWriter(), report); PlanRunnerResultAssert.assertEquals(this.getClass(), "TEST-JUnit4ReportWriterTest-testSimpleSequence-expected.xml", report, "time=\".+?\""); }