JaCoCo + maven 测试代码覆盖率 mvn -VM参数

本文详细介绍JaCoCo开源覆盖率工具的使用方法,包括如何在Maven项目中配置依赖和执行覆盖率分析,生成详细的报告,帮助开发者了解代码覆盖率情况。

JaCoCo 是一个开源的覆盖率工具

6种覆盖率统计如下

指令级覆盖(Instructions,C0coverage),分支(Branches,C1coverage)、圈复杂度(CyclomaticComplexity)、行覆盖(Lines)、方法覆盖(non-abstract methods)、类覆盖(classes)

首先在pom.xml中加入以下依赖 这个是无需加vm参数的

<build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.2</version>
                <configuration>
                    <destFile>target/coverage-reports/jacoco-unit.exec</destFile>
                    <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
                    <excludes>
                        <!--&lt;!&ndash;这个是设置屏蔽规则的地方,在分析报告中不会出现&#45;&#45;!>-->
                        <exclude>**/*TargetManagerProvider.class</exclude>
                    </excludes>
                </configuration>
                    <executions>
                        <execution>
                            <id>jacoco-initialize</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <!--这个report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
                         <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

添加依赖后,刷新maven ,然后install 如图
在这里插入图片描述
分析结果便会在相应模块下面 target/site/jacoco/index.html 如图
在这里插入图片描述
如果没有出现site 出现了coverage-reports\jacoco-unit.exec这个文件,那么可以执行report导出
在这里插入图片描述
第二种需要添加vm参数的

<build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.2</version>
                <configuration>
                    <destFile>target/coverage-reports/jacoco-unit.exec</destFile>
                    <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
                    <excludes>
                        <!--&lt;!&ndash;这个是设置屏蔽规则的地方,在分析报告中就不会出现&#45;&#45;!>-->
                        <exclude>**/*TargetManagerProvider.class</exclude>
                    </excludes>
                </configuration>
                    <executions>
                        <execution>
                            <id>jacoco-initialize</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <!--这个参数就是在下方配置VM时需要用到的-->
                            <configuration>
                            <propertyName>jacocoArgLine</propertyName>
                        </configuration>
                        </execution>
                        <!--这个report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
                         <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
               <configuration>
               <!--这个是配置VM参数的 这个 $(jacocoArgLine)必须要加上 否则会使用surefire代理 -->
                        <argLine>-Xms100m -Xmx2048m -XX:PermSize=100M -DSCRIPT_DEBUG=false -Dfile.encoding=UTF-8 ${jacocoArgLine}</argLine>
                        <forkMode>always</forkMode>
                   <skip>false</skip>
                    <testFailureIgnore>false</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

然后重复上述操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值