Creating JUnit report programmatically

2019-08-25 13:59发布

问题:

My current approach right now is from my application, just call the ant build and it will create the junit reports needed, but I want to have the flexibility to code junit reports programmatically.

I tried this approach Ant:create JUnit report task programmatically

FileSet fs = new FileSet();
fs.setDir(new File(".."));
fs.createInclude().setName("TEST-*.xml");
XMLResultAggregator aggregator = new XMLResultAggregator();
aggregator.addFileSet(fs);
AggregateTransformer transformer = aggregator.createReport();
transformer.setFormat(Format.FRAMES);
transformer.setTodir(new File("..");

but I can't even get it running. Any more ideas?

回答1:

The code you've pasted is the equivalent of calling the junitreport task:

<junitreport todir="..">
<fileset dir="..">
<include name="TEST-*.xml" />
</Fileset>
<report format="frames" todir=".." />
</Junitreport>

You need to put this into a method and run it yourself. This code will take all files called TEST-*.xml and create a report with them. It will not create those files. Those files are created by the junit task in Ant. So you need to:

  1. Run the junit task programmatically (see JUnitTask (Apache Ant API)), ensuring that the TEST*.xml files are created in a temp directory somewhere.
  2. Run the above code to produce the report, using those temp files.

The easiest way to do this is probably what you've done, to have a build.xml somewhere, and invoke ant directly. If the fileset you're using is stable, then this is probably the easiest way to go. Use the ProcessBuilder java class for this.



标签: java ant junit