java 使用 Maven 的 JUnit3 和 Junit4 XML 报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3282767/
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-10-30 01:08:17  来源:igfitidea点击:

JUnit3 and Junit4 XML Reports with Maven

javamaven-2junit4junit3

提问by Chris

I am trying to figure out how to use the supposed reporting capabilities of JUnit (3 and 4) in conjunction with Maven, but Google searches aren't turning up much in the way of how to actually run JUnit (via Maven), get a report for each test (or all tests) and what format it will be in.

我试图弄清楚如何将 JUnit(3 和 4)的假定报告功能与 Maven 结合使用,但 Google 搜索并没有在如何实际运行 JUnit(通过 Maven)方面出现太多问题,获得一个每个测试(或所有测试)的报告及其格式。

So, my multi-part questions is:

所以,我的多部分问题是:

1.) What sort of XML format is JUnit (3/4) capable of outputting?

1.) JUnit (3/4) 能够输出什么样的 XML 格式?

2.) What sort of calling convention/arguments are required for JUnit to output these reports?

2.) JUnit 需要什么样的调用约定/参数来输出这些报告?

3.) Where are the reports output?

3.) 报告输出在哪里?

4.) Can these reports be generated while running via Maven or is my only option to use a report that Maven generates?

4.) 这些报告能否在通过 Maven 运行时生成,或者是我使用 Maven 生成的报告的唯一选择?

Any links or advice would be greatly appreciated.

任何链接或建议将不胜感激。

回答by Pascal Thivent

The Maven Surefire Pluginis the plugin that runs tests and generates 2 raw reports by default:

Maven的Surefire插件是运行测试,在默认情况下产生2个的原始报告的插件:

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in 2 different file formats:

  • Plain text files (*.txt)
  • XML files (*.xml)

By default, these files are generated at ${basedir}/target/surefire-reports

Surefire 插件在构建生命周期的测试阶段用于执行应用程序的单元测试。它以 2 种不同的文件格式生成报告:

  • 纯文本文件 (*.txt)
  • XML 文件 (*.xml)

默认情况下,这些文件在 ${basedir}/target/surefire-reports

The plugin has some parameter allowing to tweak the reports a bit. From the surefire:testmojo documentation:

该插件有一些参数允许稍微调整报告。从 surefire:testmojo 文档:

  • disableXmlReport: Flag to disable the generation of report files in xml format. Default value is:false.
  • reportFormat: Selects the formatting for the test report to be generated. Can be set as brief or plain. Default value is:brief.
  • trimStackTrace: Whether to trim the stack trace in the reports to just the lines within the test, or show the full trace. Default value is:true.
  • disableXmlReport: 禁用生成 xml 格式的报告文件的标志。默认值为:false。
  • reportFormat:选择要生成的测试报告的格式。可以设置为简短或简单。默认值为:简短。
  • trimStackTrace:是否将报告中的堆栈跟踪修剪为测试中的行,或者显示完整的跟踪。默认值为:true。

For an HTML format of the report, you can then use the Maven Surefire Report Plugin:

对于 HTML 格式的报告,您可以使用Maven Surefire Report Plugin

The Surefire Report Plugin parses the generated TEST-*.xmlfiles under ${basedir}/target/surefire-reportsand renders them to DOXIA which creates the web interface version of the test results.

Surefire Report Plugin 解析下生成的TEST-*.xml文件${basedir}/target/surefire-reports并将它们呈现给 DOXIA,后者创建测试结果的 Web 界面版本。

You can either get the report generated as part of the site generation or by running the standalone surefire-report:reportgoal. From the Usagepage:

您可以将报告作为站点生成的一部分生成,也可以通过运行独立surefire-report:report目标来生成。从使用页面:

Generate the report as part of Project Reports

To generate the Surefire report as part of the site generation, add the following in the section of your POM:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.5</version>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

When the mvn siteis invoked, the report will be automatically included in the Project Reportsmenu as shown in the figure below.

alt text
(source: apache.org)

Generate the report as standalone

The Surefire report can also generate the report using its standalone goal:

mvn surefire-report:report  

A HTML report should be generated in ${basedir}/target/site/surefire-report.html.

alt text
(source: apache.org)

生成报告作为项目报告的一部分

要在站点生成过程中生成 Surefire 报告,请在 POM 部分添加以下内容:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.5</version>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

mvn site被调用时,该报告将被自动包含在项目报告如下图所示图菜单。

替代文字
(来源:apache.org

独立生成报告

Surefire 报告还可以使用其独立目标生成报告:

mvn surefire-report:report  

应生成 HTML 报告in ${basedir}/target/site/surefire-report.html

替代文字
(来源:apache.org

回答by Andy

The Maven Surefire Pluginis what executes a lot of the test reports.

Maven的Surefire插件是什么,执行了大量的测试报告。

You may also want to look at Coberturafor code coverage.

您可能还想查看Cobertura以了解代码覆盖率。