单元测试报告

简介

	最近有个项目需要我们能够出一份单元测试报告,以前都是写测试用例,直接运行查看结果,没有生成过测试报告,所以借这个机会研究了几个生成报告的插件

内容介绍

首先我们使用Juint编写测试代码,使用Maven构建项目,涉及到的插件包括
1、maven-surefire-plugin 生成报告的插件
2、maven-antrun-extended-plugin 生成比较友好的报告的插件
3、cobertura-maven-plugin 代码覆盖率插件
4、jacoco-maven-plugin 代码覆盖率插件

maven-surefire-plugin插件

配置

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <!--当单元测试不通过的时候任然生成测试报告-->
                <testFailureIgnore>true</testFailureIgnore>
                <!--表示不进行字节码验证  有些是在1.6的环境下开发,而现在使用1.8进行测试,可能会出现验证不通过的情况-->
               <!-- <argLine>-noverify -XX:-UseSplitVerifier</argLine>-->
            <!--    &lt;!&ndash;表示需要测试哪些用例   默认情况下包含test目录下*Ttest.java或者Test*.javad的类&ndash;&gt;
                <includes>
                    <include>**/*Test.java</include>
                </includes>-->
            </configuration>
        </plugin>

执行命令

mvn surefire-report:report生成html格式的报告,报告格式如下
单元测试报告

maven-antrun-extended-plugin插件

maven-surefire-plugin生成的报告格式不够友好,阅读性比较差,使用maven-antrun-extended-plugin插件可以生成比较友好的html报告

配置

	 <plugin>
            <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="${basedir}/target/surefire-reports">
                                <fileset dir="${basedir}/target/surefire-reports">
                                    <include name="**/*.xml" />
                                </fileset>
                                <report format="frames" todir="${basedir}/target/antrun-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>

执行命令 mvn test 生成html文件,文件格式如下

	![这个格式相对于上面那种,友好度好多了](https://img-blog.****img.cn/20181112152818341.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmNhbzk1Mg==,size_16,color_FFFFFF,t_70)

cobertura-maven-plugin生成代码覆盖率报告

配置

	<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.1</version>
        </plugin>

##执行命令
mvn cobertura:cobertura
执行完成后会生成html格式的覆盖率报告,如下
单元测试报告

jacoco-maven-plugin代码覆盖率报告插件

配置

	<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>target/jacoco.exec</dataFile>
                        <outputDirectory>target/jacoco-report</outputDirectory>
                    </configuration>
                </execution>
            </executions>

            <!-- Configuration 里面写配置信息 -->
            <configuration>

                <!-- rules里面指定覆盖规则 -->
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>BUNDLE</element>
                        <limits>
                            <!-- 指定方法覆盖到80% -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>METHOD</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <!-- 指定指令覆盖到80% -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>INSTRUCTION</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <!-- 指定行覆盖到80% -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <!-- 指定类覆盖到100%,不能遗失任何类 -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>CLASS</counter>
                                <value>MISSEDCOUNT</value>
                                <maximum>0</maximum>
                            </limit>

                        </limits>
                    </rule>
                </rules>
            </configuration>
        </plugin>

执行命令

	执行mvn test 后,会生成html格式的覆盖率报告,同时还会生成excel格式的报告

单元测试报告

总结

1、maven-surefire-plugin插件是基础的报告生成插件,可以生成xml、txt、html格式的报告(mvn  test命令只会生成xml、txt格式,执行mvn surefire-report:report才会生成html格式的报告)
2、maven-antrun-extended-plugin生成的html格式会更友好一些,更具可读性
3、cobertura-maven-plugin和jacoco-maven-plugin都可以生成html格式的覆盖率报告,jacoco-maven-plugin的配置更丰富,灵活性更强一些
4、其实后面的插件都是依赖maven-surefire-plugin插件而执行的
5、如果希望出现测试失败的时候也要生成报告,则设置maven-surefire-plugin: 	<testFailureIgnore>true</testFailureIgnore>即可