Jacoco 是一个免费的代码覆盖率测试工具, 分别在maven和gradle管理的项目中都可以通过简单的配置来对我们的项目代码进行单元测试用例执行覆盖率的测试, 非常的便捷好用!
常见自动化CI所采用的插件列表
gradle构建脚本使用groovy
build.gradle
中引入Jacoco插件// 引入插件
apply plugin: "jacoco"
// 以外部文件的方式引入gradle插件执行脚本, 方便对build管理, 不至于膨胀过快
apply from: 'gradle/jacoco.gradle'
jacoco.gradle
文件里面的内容// 配置jacoco的版本及检测报告目录
jacoco {
toolVersion = "0.8.4"
reportsDir = file("${buildDir}/jacoco/")
}
// test方法就是jacoco中执行单元测试生成文件后缀为exec类型的报告文件
test {
jacoco {
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
}
}
// 此方法可以生成html格式的报告, 方便开发人员查看
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination file("${buildDir}/reports/jacoco")
}
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'me/gavincook/moon/mybatis/*'
])
})
}
}
// 覆盖率检测方法, 我们可以配置我们的检测规则, 对项目覆盖率是否达标进行测试判断
jacocoTestCoverageVerification {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'me/gavincook/moon/mybatis/*'
])
})
}
violationRules {
rule {
enabled = true
limit {
minimum = 0.9
}
limit {
counter = 'BRANCH'
minimum = 0.9
}
}
}
}
// 执行test方法, 生成检测报告
gradlew test
// 执行检测方法, 判断覆盖率是否答辩
gradlew jacocoTestCoverageVerification
在项目的根目录下的.git
文件夹下面添加pre-push
文件, 这个hook就会在本地进行git push
前执行
#!/bin/sh
#set -x
# From gist at https://gist.github.com/chadmaughan/5889802
# run the tests with the gradle wrapper
./gradlew clean test
./gradlew jacocoTestReport jacocoTestCoverageVerification
# store the last exit code in a variable
RESULT=$?
# return the './gradlew jacocoTestCoverageVerification' exit code
exit $RESULT
因为hook是放在.git
文件夹下的, 而这个文件夹的内容不会被提交到远程仓库, 为了所有开发者都能以安装的方式初始化所有hook, 那我们弄个统一的脚本在每个人的本地初始化hook
boot
文件(windows下面就是boot.bat
)boot
#/bin/bash
case "$1" in
"jacoco")
open build/reports/jacoco/index.html
;;
"init-hook")
cp hook/pre-push .git/hooks/pre-push
chmod a+x .git/hooks/pre-push
;;
esac
boot.bat
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem boot.bat for windows
@rem
@rem ##########################################################################
set OPTION=%1%
set DIRNAME=%~dp0
if "%OPTION%" == "jacoco" (
start %DIRNAME%build\reports\jacoco\index.html
)
if "%OPTION%" == "init-hook" (
copy "hook\pre-push" ".git\hooks\pre-push"
echo init-hook execute success
)
// linux
boot init-hook
// windows
boot.bat init-hook
pre-push
里面的内容检测通过, 没有输出相关成功提示, 这里只看下检测不通过的错误提示
git push
[ant:jacocoReport] Rule violated for bundle moon-boot: instructions covered ratio is 0.2, but expected minimum is 0.3
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':jacocoTestCoverageVerification'.
> Rule violated for bundle moon-boot: instructions covered ratio is 0.2, but expected minimum is 0.3
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 4s
error: failed to push some refs to 'http://git.gavincook.cn/antstudio/moon-boot.git'