Generate XML Files Used by JUnit Reports

26,093

Solution 1

JUnit does not generate XML reports. There isn't a standard XML output format for JUnit.

Other tools generate XML, such as Ant/Maven. So the first thing you need to do is to decide which form of XML file you want, as in what you want to do with the files once you've created them.

And, actually, your restriction of programmatically doesn't rule out ANT. You can invoke ant programmatically (see Invoke ant from java, then return to java after ant termination). This would probably be the easiest way to generate files which are ant-compatible.

If you wish to create your own XML files, then you can create a class which extends RunListener, and then run your tests by invoking JUnitCore#run(), or similar.

public void main(String... args) {
    JUnitCore core= new JUnitCore();
    core.addListener(new RingingListener());
    core.run(MyTestClass.class);
}

Your RunListener would just emit the appropriate XML. It is fairly easy to understand: override the methods testRunStarted() etc and write out the XML. For an example of how it works, see TextListener, which does the same thing, but for text.

Solution 2

xml files are generated by ant-junit, and we can do that by program, the code will look like next :

Project project = new Project();
JUnitTask task = new JUnitTask();
project.setProperty("java.io.tmpdir",String); //set temporary directory
task.setProject(project);
JUnitTask.SummaryAttribute sa = new JUnitTask.SummaryAttribute();
sa.setValue("withOutAndErr");
task.setFork(false);
task.setPrintsummary(sa);
FormatterElement formater = new FormatterElement();         
FormatterElement.TypeAttribute type = new FormatterElement.TypeAttribute();
type.setValue("xml");
formater.setType(type);
task.addFormatter(formater);
JUnitTest test = new JUnitTest(String);// set Test.class.getname()          
test.setTodir(File); // set Location for your report
task.addTest(test);         
task.execute();

Solution 3

As being mentioned JUnit does not generate reports. But if you use Maven executing mvn clean install test surefire-report:report will do the trick

https://examples.javacodegeeks.com/core-java/junit/junit-report-generation-example/

Share:
26,093
Aazim
Author by

Aazim

Updated on July 09, 2022

Comments

  • Aazim
    Aazim almost 2 years

    I want to create test reports generated by JUnit / TestNG PROGRAMMATICALLY. So, that rules out ANT. My main concern is to generate XML files which are created by Junit while executing test cases. I'have read that RunListener could help me achieve that, but i havn't been able to figure out how ? I'm using Selenium to created my test cases.

    How can i generate XML files which are created by JUnit ?

  • Nishant Lakhara
    Nishant Lakhara over 10 years
    Which jar files did tou use? Can u please share?
  • Nishant Lakhara
    Nishant Lakhara over 10 years
    TextListener paga has been removed. Can u share some another link?
  • user2515975
    user2515975 almost 3 years
    "org.apache.ant:ant:1.10.10" and "ant:ant-junit:1.6.5"