我需要检查新 jacoco 任务的最小覆盖范围
雅科测试覆盖率验证
此任务在 3.4.1 gradle 版本中可用,jacoco 插件>= 0.6.3
我可以运行另一个任务来生成具有分支覆盖率的 html 报告,但现在我想使用该数字使构建失败。
这是我的代码
buildscript {
ext {
....
}
repositories {
mavenCentral()
maven {
....
}
}
dependencies {
.....
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'
jar {
baseName = "coverage-test"
}
dependencies {
// my dependencies
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
wrapper {
gradleVersion = '3.4.1'
}
jacoco {
toolVersion = '0.7.9'
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
}
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(
dir: it,
excludes:
[
'com/jacoco/dto/**',
'com/jacoco/configs/**',
//and others
])
})
}
}
jacocoTestCoverageVerification {
//I tried this and it didn't work
// classDirectories = files(classDirectories.files.collect {
// fileTree(
// dir: it,
// excludes:
// [
// 'com/jacoco/dto/**',
// 'com/jacoco/configs/**',
// //and others
// ])
// })
violationRules {
rule {
//Also tried this and it didn't work
// excludes = ['com/jacoco/dto/**', ...]
limit {
counter = 'BRANCH'
minimum = 0.8
}
}
}
}
check.dependsOn jacocoTestCoverageVerification
使用类目录,我收到以下错误无法在空对象上获取属性"文件"。使用第二个选项(仅排除(,构建运行顺利,但不排除任何类。
就我而言,我确实想使用 BUNDLE 范围为整体设置阈值,同时排除某些包和文件。
最终对我有用的是添加classDirectories
排除,如原始问题中所建议的那样,但在afterEvaluate
内部是这样的:
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'com/example/my/package/*',
'com/example/service/MyApplication.class',
'com/google/protobuf/*'
])
})
}
作为参考,完整的build.gradle
如下所示:
apply plugin: "jacoco”
jacocoTestCoverageVerification {
afterEvaluate {
getClassDirectories().setFrom(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'com/example/my/package/*',
'com/example/service/MyApplication.class',
'com/google/protobuf/*'
])
})
}
violationRules {
rule {
limit {
minimum = 0.79
}
}
}
}
// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification
您可以在我的博客中找到更多详细信息:http://jivimberg.io/blog/2018/04/26/gradle-verify-coverage-with-exclusions/
您正在测量您正在排除的不同事物。默认的JaCoCo范围是"BUNDLE",我相信这意味着整个代码。我从来没有用过那个。我总是只测量"类"范围。看起来你也在尝试做同样的事情。
排除项相对于作用域中的元素。不确定它对"捆绑"意味着什么,但我几乎倾向于认为它要么全有,要么全无。排除也使用不同类型的通配符。尝试更改配置以使用元素"CLASS"(或"PACKAGE"(。
violationRules {
rule {
element = 'CLASS'
excludes = ['com.jacoco.dto.*']
limit {
counter = 'BRANCH'
minimum = 0.8
}
}
}
check.dependsOn jacocoTestCoverageVerification