Maven 单元测试覆盖率检测插件 JaCoCo

Maven About 2,513 words

配置插件

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
    <configuration>
        <!-- 指定生成 .exec 文件的存放位置 -->
        <destFile>target/coverage-reports/jacoco-unit.exec</destFile>
        <!-- JaCoCo 是根据 .exec 文件生成最终的报告,所以需指定 .exec 的存放路径-->
        <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
        <!-- rules 里面指定覆盖规则 -->
        <rules>
            <rule implementation="org.jacoco.maven.RuleConfiguration">
                <element>BUNDLE</element>
                <limits>  
                    <!-- 指定方法覆盖到 50% -->
                    <limit implementation="org.jacoco.report.check.Limit">
                        <counter>METHOD</counter>
                        <value>COVEREDRATIO</value>
                        <!-- 最低覆盖率 -->
                        <minimum>0.50</minimum>
                    </limit>
                    <!-- 指定分支覆盖到 50% -->
                    <limit implementation="org.jacoco.report.check.Limit">
                        <counter>BRANCH</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.50</minimum>
                    </limit>
                    <!-- 指定类覆盖到 100%,不能遗失任何类 -->
                    <limit implementation="org.jacoco.report.check.Limit">
                        <counter>CLASS</counter>
                        <value>MISSEDCOUNT</value>
                        <maximum>0</maximum>
                    </limit>
                </limits>
            </rule>
        </rules>
    </configuration>
    <executions>
        <execution>
            <id>pre-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

执行测试

mvn clean test

生成的报告位置

target/site/jacoco/index.html

参考

https://www.jacoco.org/jacoco/trunk/doc/maven.html

Views: 1,421 · Posted: 2023-03-19

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh