用maven生成Junit test report

看到别人用ant生成了junit test report,而自己的maven2只在target的surefire-reports中生成了一堆的txt/xml文件。打开pom.xml发现:surefire-reports被加在了build中。把这段设置去掉,在reporting中加入:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>
于是surefire-report.html在site中生成了,同时依然会生成surefire-reports。
当然还可以设置一些configuration。
上面是针对maven2;maven1中相应的plugin是:maven-junit-report-plugin。
### 如何在 IntelliJ IDEA 中生成 JUnit 测试报告 在 IntelliJ IDEA 中生成 JUnit 测试报告可以通过多种方式实现,以下是具体的方法: #### 配置测试环境 为了能够正常运行并生成测试报告,首先需要确保项目的依赖配置正确。如果遇到 `org.junit` 不可见的情况,则可以按照以下步骤解决[^2]: 1. 打开 **File → Project Structure → Libraries**。 2. 点击左上角的加号 (`+`) 并选择 **From Maven**。 3. 输入 `junit:junit:4.12` 或更高版本,并点击确认。 此外,在基于 Gradle 的项目中,可以在 `build.gradle` 文件中添加如下依赖项以引入 JUnit 库: ```gradle dependencies { testImplementation 'junit:junit:4.12' } ``` 完成上述操作后同步项目即可加载必要的库支持。 #### 自动生成测试类 对于希望快速创建针对某个类的单元测试场景,可利用 IntelliJ 提供的功能简化流程。例如按下组合键 **Ctrl + Shift + T** 后选择目标位置新建对应的 Test Class[^3]。或者借助第三方插件如 **JUnitGenerator** 实现更灵活定制化需求(通过 Alt+Insert 快捷键调用),此时需注意设置好默认存储路径以便后续管理方便[^1]。 #### 使用 Surefire 插件生成 HTML 报告 Maven 用户推荐采用 Apache Maven Surefire Plugin 来执行自动化构建过程中的所有测试并将结果导出成易于阅读的形式。其基本原理在于定义一组 XML 结构描述符用于记录每次运行期间发生的事件详情以及最终状态总结信息等内容。 要在 pom.xml 添加下面片段启用该功能: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <!-- 输出详细的控制台日志 --> <useFile>false</useFile> <includes> <include>**/*Test.java</include> </includes> <reportsDirectory>${project.build.directory}/surefire-reports-html</reportsDirectory> <reportFormat>plain</reportFormat> </configuration> </plugin> </plugins> </build> ``` 之后只需运行命令 mvn clean verify 即可在 target/surefire-reports 下找到 html 版本的结果页面文件夹。 #### 利用 Ant Task 创建 PDF 文档形式报表 另一种常见做法就是依靠 ant build script 完全掌控整个编译链接直至打包发布全过程的同时也负责处理有关质量度量方面的事务比如覆盖率统计等等。这里我们仅讨论关于单纯获取 pass/fail 记录方面的工作。 编辑 build.xml 加入类似这样的 task definition : ```ant <junit printsummary="yes" haltonfailure="no"> <classpath refid="test.classpath"/> <formatter type="brief" usefile="false"/> <batchtest fork="yes" todir="${basedir}/test-report"> <fileset dir="${src.dir}"> <include name="**/*Test*.java"/> </fileset> </batchtest> </junit> <!-- 将 xml 转换成 pdf --> <xslt in="${basedir}/test-report/TESTS-TestSuites.xml" style="${basedir}/tools/junit-frames.xsl" out="${basedir}/test-report/index.html"/> <target name="generate-pdf-from-tests"> <exec executable="w3m"> <arg value="-dump"/> <arg value="${basedir}/test-report/index.html"/> <redirector output="${basedir}/test-report/report.txt"/> </exec> <exec executable="enscript"> <arg value="--color"/> <arg value="-p${basedir}/test-report/report.ps"/> <arg value="${basedir}/test-report/report.txt"/> </exec> <exec executable="ps2pdf"> <arg value="${basedir}/test-report/report.ps"/> <arg value="${basedir}/test-report/report.pdf"/> </exec> </target> ``` 以上脚本会先做常规的单元检验动作接着把原始数据转换为 web 友好的超文本标记语言表述最后再进一步加工成为便于打印携带保存分享传播的标准便携文档格式产物。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值