【Maven】单元测试、统计、覆盖率相关插件使用介绍

maven-surefire-plugin

  • maven-surefire-pluginmaven执行单元测试的插件,不显性配置也可以直接使用。
  • 这个插件的surefire:test命令会默认绑定maven执行的test阶段。
  • 执行结束后,默认在target/surefire-reports目录下会生成txtxml两种格式的结果,不利于直观展示,需要结合其它插件一起使用。

如果你自己声明了,那么可以指定自己的版本,并且可以配置自定义的参数。

配置示例

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!-- 是否忽略单元测试 -->
        <skipTests>false</skipTests>
        <!-- 是否忽略执行执行的单元测试 -->
        <testFailureIgnore>true</testFailureIgnore>
        <argLine>${argLine}</argLine>
    </configuration>
</plugin>
  • argLine参数说明
  • jacoco:report-aggregate聚合报告后,覆盖率显示为0,与这个参数有关
  • jacoco在prepare-agent阶段会生成一个属性指向jacoco的runtime agent,默认这个属性叫argLine
  • 需要在maven-surefire-plugin的配置中,引用这个属性

maven-surefire-report-plugin

为单元测试生成html报告,默认位置target/site/surefire-report.html

配置示例

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-report-plugin</artifactId>
    <version>${maven-surefire-report-plugin.version}</version>
    <executions>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

执行mvn test时,会生成报告

效果预览

在这里插入图片描述

jacoco-maven-plugin

用于生成代码覆盖率报告,可以帮助我们了解代码中哪些部分已经被测试覆盖,哪些部分需要更多的测试,默认位置target/site/jacoco/index.html

配置示例

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- 需要修改maven-surefire-plugin插件的<argLine>配置,默认是argLine -->
                <propertyName>argLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--是否使用追加的模式,如果为false,新的报告会替换旧的报告-->
        <append>false</append>
        <!-- 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>

效果展示

在这里插入图片描述

如果覆盖率显示为0,需要修改maven-surefire-plugin插件的配置

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太空眼睛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值