Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate XML Files Used by JUnit Reports

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 ?

like image 495
Aazim Avatar asked Jan 30 '12 10:01

Aazim


2 Answers

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/

like image 191
Ian Kundil Avatar answered Oct 07 '22 20:10

Ian Kundil


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.

like image 41
Matthew Farwell Avatar answered Oct 07 '22 19:10

Matthew Farwell