Jacoco 代码测试覆盖率检测插件的配置和使用

Jacoco 是一个免费的代码覆盖率测试工具, 分别在maven和gradle管理的项目中都可以通过简单的配置来对我们的项目代码进行单元测试用例执行覆盖率的测试, 非常的便捷好用!


常见自动化CI所采用的插件列表

在项目中配置使用Jacoco

在Gradle项目中, 配置使用Jacoco

gradle构建脚本使用groovy

  1. 首先在build.gradle中引入Jacoco插件
// 引入插件
apply plugin: "jacoco"

// 以外部文件的方式引入gradle插件执行脚本, 方便对build管理, 不至于膨胀过快
apply from: 'gradle/jacoco.gradle'
  1. 看一下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
            }
        }
    }
}

  1. 在命令行终端执行以下命令对插件进行使用
// 执行test方法, 生成检测报告
gradlew test
// 执行检测方法, 判断覆盖率是否答辩
gradlew jacocoTestCoverageVerification

在git管理的项目中, 通过hook执行jacoco verify

在项目的根目录下的.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

因为hook是放在.git文件夹下的, 而这个文件夹的内容不会被提交到远程仓库, 为了所有开发者都能以安装的方式初始化所有hook, 那我们弄个统一的脚本在每个人的本地初始化hook

  1. 在项目根目录下面建一个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
)
  1. 执行脚本初始化方法
// linux
boot init-hook
// windows
boot.bat init-hook

本地进行git push时会自动执行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'

报告如图
jacoco report
Over!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章