我试图弄清楚如何将 JUnit(3 和 4)的假定报告功能与 Maven 结合使用,但谷歌搜索并没有在如何实际运行 JUnit(通过 Maven)方面出现太多问题,获取每个测试(或所有测试)的报告及其格式。
所以,我的多部分问题是:
1.) JUnit (3/4) 能够输出什么样的 XML 格式?
2.) JUnit 需要什么样的调用约定/参数来输出这些报告?
3.) 报告输出在哪里?
4.) 这些报告可以在通过 Maven 运行时生成,还是我使用 Maven 生成的报告的唯一选择?
如有任何链接或建议,我们将不胜感激。
最佳答案
Maven Surefire Plugin是运行测试并默认生成 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:test
mojo 文档:
-
disableXmlReport
: 标志以禁止生成 xml 格式的报告文件。 默认值为: false。 -
reportFormat
:选择要生成的测试报告的格式。可以设置为简短或简单。 默认值为:简短。 -
trimStackTrace
:是将报告中的堆栈跟踪修剪为仅测试中的行,还是显示完整跟踪。 默认值为: true。
对于报告的 HTML 格式,您可以使用 Maven Surefire Report Plugin :
The Surefire Report Plugin parses the generated
TEST-*.xml
files under${basedir}/target/surefire-reports
and renders them to DOXIA which creates the web interface version of the test results.
您可以获取作为站点生成的一部分生成的报告,也可以通过运行独立的 surefire-report:report
目标来获取。来自Usage页:
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 site
is invoked, the report will be automatically included in the Project Reports menu as shown in the figure below.
(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
.
(source: apache.org)
关于java - 使用 Maven 的 JUnit3 和 Junit4 XML 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3282767/