生成 JUnit 报告使用的 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9062412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:08:20  来源:igfitidea点击:

Generate XML Files Used by JUnit Reports

xmltestingseleniumjunittestng

提问by Aazim

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.

我想创建由 JUnit / TestNG PROGRAMMATICALLY 生成的测试报告。所以,这排除了ANT。我主要关心的是生成由 Junit 在执行测试用例时创建的 XML 文件。我读过 RunListener 可以帮助我实现这一目标,但我无法弄清楚如何实现?我正在使用 Selenium 创建我的测试用例。

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

如何生成由 JUnit 创建的 XML 文件?

回答by Matthew Farwell

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

JUnit 不生成 XML 报告。JUnit 没有标准的 XML 输出格式。

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.

其他工具生成 XML,例如 Ant/Maven。因此,您需要做的第一件事是决定您想要哪种形式的 XML 文件,就像创建文件后要对它们做什么一样。

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.

而且,实际上,您对编程的限制并不排除 ANT。您可以以编程方式调用 ant(请参阅从 java 调用 ant,然后在 ant 终止后返回 java)。这可能是生成与 ant 兼容的文件的最简单方法。

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.

如果您希望创建自己的 XML 文件,那么您可以创建一个扩展RunListener的类,然后通过调用JUnitCore#run()或类似方法运行您的测试。

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.

您的 RunListener 只会发出适当的 XML。很容易理解:覆盖方法 testRunStarted() 等并写出 XML。有关其工作原理的示例,请参阅TextListener,它执行相同的操作,但用于文本。

回答by freesky

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

.xml 文件是由 ant-junit 生成的,我们可以通过程序来完成,代码如下所示:

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();

回答by Tarken

You asked nearly the same hereIf you look at the TestNG doc you can use :

您在这里问的几乎相同 如果您查看 TestNG 文档,您可以使用:

The org.testng.IReporter interface which only has one method: public void generateReport(List suites, String outputDirectory) This method will be invoked by TestNG when all the suites have been run and you can inspect its parameters to access all the information on the run that was just completed.

org.testng.IReporter 接口只有一个方法: public void generateReport(List suites, String outputDirectory) 这个方法会在所有套件都运行后被TestNG调用,你可以检查它的参数来访问所有的信息刚刚完成的运行。