Creating XML Files Used by JUnit Reports

I want to create test reports created by JUnit / TestNG PROGRAMMATICALLY. So this excludes ANT. My main task is to generate XML files created by Junit during the execution of test cases. I read that RunListener can help me with this, but I could not figure out how to do this? I use Selenium to create tests.

How can I generate XML files created by JUnit?

+8
xml junit selenium testng testing
source share
3 answers

JUnit does not generate XML reports. There is no standard XML output format for JUnit.

Other tools generate XML, such as Ant / Maven. So, the first thing you need to do is decide what form of the XML file you want, as in what you want to do with the files after they are created.

And, in fact, your restriction programmatically does not exclude ANT. You can program ant programmatically (see Call ant from java and then return to java after ant completes ). This would probably be the easiest way to generate ant -compatible files.

If you want to create your own XML files, you can create a class that extends RunListener and then runs your tests by calling JUnitCore # run () or similar.

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

Your RunListener will just emit the appropriate XML. This is pretty easy to understand: override testRunStarted () methods, etc. And write out XML. For an example of how this works, see TextListener , which does the same for text.

+9
source share

xml files are created using ant -junit, and we can do this using a program, the code will look like this:

 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(); 
+4
source share

You asked almost the same here. If you look at the TestNG document, you can use:

The org.testng.IReporter interface, which has only one method: public void generateReport (List suites, String outputDirectory) This method will be called by TestNG when all the packages are running, and you can check its parameters to get access to all the information about the run which has just been completed.

0
source share

All Articles