Creating a JUnit Report Programmatically

My current approach is right now from my application, just call the ant construct and it will generate unit reports, but I want to have the flexibility to programmatically report unit reports.

I tried this Ant approach : create a 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 start it. Any ideas?

0
source share
1 answer

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

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

You need to put this in a method and run it yourself. This code will accept all files with the name TEST-*.xml and create a report with them. He will not create these files. These files are created using the junit task in Ant. Therefore you need to:

  • Programmatically execute the junit task (see JUnitTask (Apache Ant API) ), ensuring that TEST * .xml files are created somewhere in the temp directory somewhere.
  • Run the code above to create the report using these temporary files.

The easiest way to do this is probably what you did, have build.xml somewhere and call Ant directly. If the set of files you use is stable, then this is probably the easiest way. Use this class ProcessBuilder .

+1
source

All Articles