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!

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