我如何验证某些排除的 类 和 jacoco 插件的最小覆盖率?
How can I verify the minimum coverage with some excluded classes and with the jacoco plugin?
我需要用新的 jacoco 任务检查最小覆盖率
jacocoTestCoverageVerification
此任务在 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
使用 classDirectories 我得到以下错误 Cannot get 属性 'files' on null object.使用第二个选项(仅 排除 ),构建 运行 顺利但不排除任何 class.
您测量的是您排除的不同事物。默认的 JaCoCo 范围是 "BUNDLE",我相信这意味着整个代码。我从来没有用过那个。我总是只测量 "CLASS" 范围。看起来你也在尝试做同样的事情。
排除是相对于范围内的元素而言的。不确定它对 "BUNDLE" 意味着什么,但我几乎倾向于认为它要么全有要么全无。此外,排除使用不同类型的通配符。尝试更改您的配置以使用元素 "CLASS"(或 "PACKAGE")。
violationRules {
rule {
element = 'CLASS'
excludes = ['com.jacoco.dto.*']
limit {
counter = 'BRANCH'
minimum = 0.8
}
}
}
check.dependsOn jacocoTestCoverageVerification
在我的例子中,我 did 想使用 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 任务检查最小覆盖率
jacocoTestCoverageVerification
此任务在 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
使用 classDirectories 我得到以下错误 Cannot get 属性 'files' on null object.使用第二个选项(仅 排除 ),构建 运行 顺利但不排除任何 class.
您测量的是您排除的不同事物。默认的 JaCoCo 范围是 "BUNDLE",我相信这意味着整个代码。我从来没有用过那个。我总是只测量 "CLASS" 范围。看起来你也在尝试做同样的事情。
排除是相对于范围内的元素而言的。不确定它对 "BUNDLE" 意味着什么,但我几乎倾向于认为它要么全有要么全无。此外,排除使用不同类型的通配符。尝试更改您的配置以使用元素 "CLASS"(或 "PACKAGE")。
violationRules {
rule {
element = 'CLASS'
excludes = ['com.jacoco.dto.*']
limit {
counter = 'BRANCH'
minimum = 0.8
}
}
}
check.dependsOn jacocoTestCoverageVerification
在我的例子中,我 did 想使用 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/