Java 是否有适用于 Maven 的体面的 HTML Junit 报告插件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2846493/
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
Is there a decent HTML Junit report plugin for Maven?
提问by Y.H Wong
I find the surefire-report
plug-in very unsuitable to my working style. I clean the project all the time and I don't want to spend 5 min to rebuild the whole site every time I want to look at the test report in my browser.
我发现surefire-report
插件非常不适合我的工作方式。我一直在清理项目,我不想每次想在浏览器中查看测试报告时都花费 5 分钟来重建整个站点。
If I type mvn surefire-report:report-only
, the generated report is too ugly and barely readable.
如果我输入mvn surefire-report:report-only
,生成的报告太难看,几乎不可读。
What I'm looking for is something like ant's JUnitReport task. Is there one available out there already?
我正在寻找的是类似于 ant 的 JUnitReport 任务。有没有可以用的?
采纳答案by Pascal Thivent
Indeed, generating the whole site at each build is clearly not an option. But the problem is that mvn surefire-report:report-only
doesn't create the the css/*.css files, hence the ugly result. This is logged in SUREFIRE-616(doesn't mean something will happen though). Personally, I don't use HTML reports that much so I can live with that but that's not a good answer so here is a workaround based on the ant task (*sigh*):
事实上,在每次构建时生成整个站点显然不是一种选择。但问题是mvn surefire-report:report-only
不会创建 css/*.css 文件,因此结果很难看。这记录在SUREFIRE-616 中(但这并不意味着会发生什么)。就我个人而言,我不怎么使用 HTML 报告,所以我可以接受,但这不是一个好的答案,所以这里有一个基于 ant 任务的解决方法 (*sigh*):
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>test-reports</id>
<phase>test</phase>
<configuration>
<tasks>
<junitreport todir="target/surefire-reports">
<fileset dir="target/surefire-reports">
<include name="**/*.xml"/>
</fileset>
<report format="noframes" todir="target/surefire-reports"/>
</junitreport>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
</plugin>
Update:My initial idea was to run the Maven AntRun plugin "on demand" to generate the reports... but that's not what I posted, I bound it to the test
phase... But I didn't think about the case of failed tests (that would stop the build and prevent the execution of the AntRun plugin). So, either:
更新:我最初的想法是“按需”运行Maven AntRun插件来生成报告......但这不是我发布的,我把它绑定到了test
阶段......但我没有考虑失败的情况测试(这将停止构建并阻止 AntRun 插件的执行)。所以,要么:
Don't bind the AntRun plugin to the
test
phase, move the configuration outside theexecution
and callmvn antrun:run
on the command line to generate the reports when wanted.or use the
testFailureIgnore
option of the test mojo and set it to true in the surefire plugin configuration:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin>
or set this expression from the command line using the -D parameter:
$ mvn test -Dmaven.test.failure.ignore=true
不要将 AntRun 插件绑定到
test
阶段,将配置移到阶段之外execution
并mvn antrun:run
在命令行上调用以在需要时生成报告。或者使用
testFailureIgnore
test mojo的选项并在surefire插件配置中将其设置为true:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin>
或使用 -D 参数从命令行设置此表达式:
$ mvn test -Dmaven.test.failure.ignore=true
I think that Option #1 is the best option, you don't necessarily want to generate the reports (especially when the test passes) and generate them systematically may slow down the build on the long term. I'd generate them "on demand".
我认为选项 #1 是最好的选择,您不一定要生成报告(尤其是当测试通过时)并系统地生成它们可能会减慢长期的构建速度。我会“按需”生成它们。
回答by Y.H Wong
Thanks for Pascal, I've found an improved solution to do what I want to do:
感谢 Pascal,我找到了一个改进的解决方案来做我想做的事情:
<plugin>
<!-- Extended Maven antrun plugin -->
<!-- https://maven-antrun-extended-plugin.dev.java.net/ -->
<groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
<artifactId>maven-antrun-extended-plugin</artifactId>
<executions>
<execution>
<id>test-reports</id>
<phase>test</phase>
<configuration>
<tasks>
<junitreport todir="target/surefire-reports">
<fileset dir="target/surefire-reports">
<include name="**/*.xml"/>
</fileset>
<report format="noframes" todir="target/surefire-reports"/>
</junitreport>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-trax</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</plugin>
This version uses a newer version of ant and best of all. However, I still haven't found a way to generate a test report when tests fail. How should I do that?
此版本使用较新版本的 ant,最重要的是。但是,我仍然没有找到在测试失败时生成测试报告的方法。我该怎么做?
回答by Y.H Wong
You can set -Dmaven.test.failure.ignore=true
to generate the test report when tests fail.
您可以设置-Dmaven.test.failure.ignore=true
在测试失败时生成测试报告。
回答by djangofan
Here is how I did it using the goal site maven-surefire:report:
这是我如何使用目标站点 maven-surefire:report做到的:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.16</version>
<configuration>
<showSuccess>false</showSuccess>
<outputDirectory>${basedir}/target/surefire-reports</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<configuration>
<outputDirectory>${basedir}/target/surefire-reports</outputDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
回答by prasanna
This is what I do:
这就是我所做的:
# Run tests and generate .xml reports
mvn test
# Convert .xml reports into .html report, but without the CSS or images
mvn surefire-report:report-only
# Put the CSS and images where they need to be without the rest of the
# time-consuming stuff
mvn site -DgenerateReports=false
go to target/site/surefire-report.html for the report.
转到 target/site/surefire-report.html 获取报告。
After tests run, the rest of the two run in about 3.5 seconds for me.
测试运行后,其余的两个运行对我来说大约需要 3.5 秒。
Hope that helps. Enjoy!
希望有帮助。享受!
回答by Sach_21590
Create a new maven run configuration and with goal =>
创建一个新的 maven 运行配置并使用目标 =>
surefire-report:report site -DgenerateReports=false
This can help you to have a better report view with css.
这可以帮助您使用 css 获得更好的报告视图。
回答by Ravikiran Reddy Kotapati
Run the below command
运行以下命令
mvn clean install surefire-report:report
You can find the report in the below location
您可以在以下位置找到报告
{basedir}/target/site/surefire-report.html
For more details refer the below link
有关更多详细信息,请参阅以下链接
http://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html
http://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html
回答by suresh chandra
Add this pom as above mentioned 1.option take out the configuration from execution phase keep outside, run mvn 'verify'and then run again mvn 'antrun:run'.. Then you are able to see failed test cases as well
添加上面提到的这个 pom 1.option 从执行阶段取出配置放在外面,运行mvn 'verify'然后再次运行mvn 'antrun:run'.. 然后你也可以看到失败的测试用例
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>test-reports</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<tasks>
<junitreport todir="target/surefire-reports">
<fileset dir="target/surefire-reports">
<include name="**/*.xml" />
</fileset>
<report format="noframes" todir="target/surefire-reports" />
</junitreport>
</tasks>
</configuration>
<dependencies>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-junit</artifactId>
<version>version</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>version</version>
</plugin>