以编程方式创建 JUnit 报告 [英] Creating JUnit report programmatically

查看:23
本文介绍了以编程方式创建 JUnit 报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的方法来自我的应用程序,只需调用 ant build 即可创建所需的 junit 报告,但我希望能够灵活地以编程方式对 junit 报告进行编码.

My current approach right now is from my application, just call the ant build and it will create the junit reports needed, but I want to have the flexibility to code junit reports programmatically.

我尝试过这种方法Ant:以编程方式创建 JUnit 报告任务

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 get it running. Any more ideas?

推荐答案

您粘贴的代码相当于调用 junitreport 任务:

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

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

你需要把它放到一个方法中并自己运行.此代码将获取所有名为 TEST-*.xml 的文件并使用它们创建报告.它不会创建这些文件.这些文件是由 Ant 中的 junit 任务创建的.所以你需要:

You need to put this into a method and run it yourself. This code will take all files called TEST-*.xml and create a report with them. It will not create those files. Those files are created by the junit task in Ant. So you need to:

  1. 以编程方式运行 junit 任务(参见 JUnitTask (Apache Ant API)),确保在某个临时目录中创建 TEST*.xml 文件.
  2. 运行上述代码以使用这些临时文件生成报告.
  1. Run the junit task programmatically (see JUnitTask (Apache Ant API)), ensuring that the TEST*.xml files are created in a temp directory somewhere.
  2. Run the above code to produce the report, using those temp files.

最简单的方法可能就是您所做的,在某处放置一个 build.xml,然后直接调用 ant.如果您使用的文件集是稳定的,那么这可能是最简单的方法.为此,请使用 ProcessBuilder java 类.

The easiest way to do this is probably what you've done, to have a build.xml somewhere, and invoke ant directly. If the fileset you're using is stable, then this is probably the easiest way to go. Use the ProcessBuilder java class for this.

这篇关于以编程方式创建 JUnit 报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆