官网配置:https://www.jacoco.org/jacoco/trunk/doc/maven.html
jacoco配置
pom文件中引入jacoco依赖
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
</dependency>
引入maven-surefire-plugin插件:
Maven本身并不是一个单元测试框架,Java世界中主流的单元测试框架为JUnit和TestNG。Maven所做的只是在构建执行到特定生命周期阶段的时候,通过插件来执行JUnit或者TestNG的测试用例。这一插件就是maven-surefire-plugin,可以称之为测试运行器(Test Runner),他能很好的兼容JUnit 3、JUnit 4以及TestNG。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<!-- 扫描所有的测试类-->
<includes>
<include>**/*Test.java</include>
</includes>
<!-- 排除测试启动类,否则运行test的时候会报错-->
<excludes>
<exclude>**/ApplicationTest.java</exclude>
</excludes>
</configuration>
</plugin>
引入jacoco-maven-plugin插件:
总体配置如下:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<configuration>
<!-- 配置的扫描framework下所有的类,这里也可以限制新增的类-->
<includes>
<include>**/thingjs/framework/**</include>
</includes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<!-- 此处不配置的话,生成不了site文件夹-->
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- 限制FW的新增类-->
<includes>
<include>**/thingjs/framework/**/Fw*.class</include>
</includes>
</configuration>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<!-- Sonar中要求有具体的check规则,不然会报错,但是通过率不能太高,否则install的时候会失败-->
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
上面具体有3个阶段,官网上写的很详细了,具体怎么配置,可以查看
prepare-agent:可以见https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html
report:https://www.jacoco.org/jacoco/trunk/doc/report-mojo.html
check:https://www.jacoco.org/jacoco/trunk/doc/check-mojo.html
测试
执行mvn test,就会出现下面的结果
maven多模块配置
通过shell获取csv中的数据进行实时计算
#!/bin/bash
:<<EOF
作者: WGR
时间: 2021/07/05
作用:计算web和svc的单元测试覆盖率,输出变量到README.md中
EOF
# web的总行数
webinstructions=`awk -F"," ‘{ instructions += $4 + $5} END { print instructions}‘ /builds/gHPfsBe5/0/uino-framework/framework/framework-web/target/site/jacoco/jacoco.csv`
# web的覆盖行数
webcovered=`awk -F"," ‘{ covered += $5} END { print covered}‘ /builds/gHPfsBe5/0/uino-framework/framework/framework-web/target/site/jacoco/jacoco.csv`
# svc的总行数
svcinstructions=`awk -F"," ‘{ instructions += $4 + $5} END { print instructions}‘ /builds/gHPfsBe5/0/uino-framework/framework/framework-service/target/site/jacoco/jacoco.csv`
# svc的覆盖行数
svccovered=`awk -F"," ‘{ covered += $5} END { print covered}‘ /builds/gHPfsBe5/0/uino-framework/framework/framework-service/target/site/jacoco/jacoco.csv`
# 输出单元测试覆盖率
awk ‘BEGIN{print (‘$webcovered‘+‘$svccovered‘)/(‘$svcinstructions‘+‘$webinstructions‘)*100 , "% covered" }‘