单元测试是保证代码质量的重要一环,而如何衡量单元测试写得好很差呢?覆盖率(Coverage)
是一个重要指标。而JaCoCo
则是专门为Java
提供的用于检测测试覆盖率的工具,英文全称为Java Code Coverage
。html
本文将讲解如何在Maven
项目中整合JaCoCo
,并在SonarQube
中展现。SonarQube
的安装能够参考这篇文章:sql
《Docker搭建代码检测平台SonarQube并检测maven项目》docker
这里所讲的覆盖率
是指测试代码的覆盖率,这个指标有多种计算方式,以下是比较经常使用的有:数据库
行覆盖率:执行代码行数 / 总代码行数,判断有多少行代码被测试执行;浏览器
类覆盖率:执行的类 / 代码中类总个数;bash
分支覆盖率:执行的逻辑分支数 / 总的分支数,通常用于检测是否是lf/else
都有测试覆盖;微信
方法覆盖率:执行的方法数 / 代码总方法数,检测是否有方法被遗漏,构造方法也看做为方法。app
圈复杂度:用于判断代码结构的复杂程序,JaCoCo
不考虑异常处理的分支;通常认为圈复杂度大于10,就存在比较大的风险,严格要求不可大于15。maven
颜色标识:工具
JaCoCo
会经过颜色来标识代码覆盖状况,使其一目了然。红色表示没有覆盖,绿色表示已经覆盖,黄色表示部分覆盖。
执行方式:
执行JaCoCo
有多种方式:
(1)直接经过命令执行:https://www.eclemma.org/jacoco/trunk/doc/agent.html
(2)Ant执行:https://www.eclemma.org/jacoco/trunk/doc/ant.html
(3)Maven执行:https://www.eclemma.org/jacoco/trunk/doc/maven.html
(4)集成IDE执行:https://www.eclemma.org/
咱们接下来主要讲解maven
的方式。
Maven
整合JaCoCo
也容易,配置以下:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <configuration> <skip>${maven.test.skip}</skip> <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile> <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile> <output>file</output> <append>true</append> <excludes> <exclude>com/pkslow/basic/containsperformance/**</exclude> <exclude>com/pkslow/basic/ReadPropertiesFile</exclude> </excludes> </configuration> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> <phase>test-compile</phase> </execution> <execution> <id>jacoco-site</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
执行mvn clean test
,则会生成报告target/coverage-reports/jacoco-unit.exec
,但这是人不可读的,Sonar
可读的。Intellij Idea
也能够阅读,按照Run--Show Code Coverage Data
打开便可。
执行mvn clean verify
,就会生成报告target/site/jacoco/
,有多种格式,用浏览器打开index.html
文件能够方便查看。以下图所示:
指定某些类不执行检测:
<excludes> <exclude>com/pkslow/basic/containsperformance/**</exclude> <exclude>com/pkslow/basic/ReadPropertiesFile</exclude> </excludes>
Rules
标签能够指定检查阈值,好比类覆盖率必须为100%
。在configuration
里面配置以下:
<rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>METHOD</counter> <value>COVEREDRATIO</value> <minimum>0.50</minimum> </limit> <limit implementation="org.jacoco.report.check.Limit"> <counter>BRANCH</counter> <value>COVEREDRATIO</value> <minimum>0.50</minimum> </limit> <limit implementation="org.jacoco.report.check.Limit"> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules>
这时须要有下面的check
才会执行这个规则校验:
<execution> <id>check</id> <goals> <goal>check</goal> </goals> </execution>
若是不知足条件,maven build
就会失败。不过,若是咱们集成了SonarQube
,咱们则会经过SonarQube
来设置这个规则和阈值。
添加SonarQube
配置信息以下,有三种配置方式:
(1)配置数据库信息
<profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:postgresql://localhost/sonar</sonar.jdbc.url> <sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver> <sonar.jdbc.username>user</sonar.jdbc.username> <sonar.jdbc.password>password</sonar.jdbc.password> <sonar.host.url>http://localhost:9000</sonar.host.url> </properties> </profile> </profiles>
(2)配置用户名密码
<profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.host.url>http://localhost:9000</sonar.host.url> <sonar.login>admin</sonar.login> <sonar.password>admin</sonar.password> </properties> </profile> </profiles>
(3)配置令牌
<profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.host.url>http://localhost:9000</sonar.host.url> <sonar.login>9656c84090b2481db6ea97b6d14d87d546bff619</sonar.login> </properties> </profile> </profiles>
以上三种均可以,配置完成后,执行命令以下:
mvn clean verify sonar:sonar
若是不想添加配置,能够直接经过命令来指定,命令以下:
mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=9656c84090b2481db6ea97b6d14d87d546bff619
JaCoCo
对项目质量管理做用重大,应该加以使用。最终的maven配置文件pom.xml
行数太大,请到( https://www.pkslow.com/archives/maven-jacoco-sonar )参考。
欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章!
欢迎关注微信公众号<南瓜慢说>,将持续为你更新...
多读书,多分享;多写做,多整理。