将 JaCoCo 与 Gradle 和 Kotlin 一起使用时如何忽略方法?
How to ignore methods when using JaCoCo with Gradle and Kotlin?
我正在尝试使用 Gradle 和 Kotlin 设置 JaCoCo,我的问题是我有很多 data class
es,它们具有编译器生成的 equals
、hashCode
和 toString
方法。
我在文档中读到,使用 JaCoCo 我可以使用 ignored 方法,但 JaCoCo 的 Gradle 插件似乎只支持 排除s。我该如何解决这个问题?
我试过这个:
test {
jacoco {
exclude("*equals", "*hashCode")
}
}
但我仍然可以在这些方法旁边看到 0%。
我做错了什么?
如JaCoCo version 0.8.1: the only thing you need to do - is to make sure that Gradle uses correct JaCoCo version. All filters implemented so far in JaCoCo are enabled unconditionally and take place during generation of report. In the same announcement and in announcement of 0.8.2公告中所述,您可以看到
With Gradle JaCoCo Plugin you can select both runtime and version for "JaCoCoReport" task using "toolVersion" - https://docs.gradle.org/current/userguide/jacoco_plugin.html
并且该默认版本取决于 Gradle 的版本 - 例如 Gradle 4.7 by default uses version JaCoCo 0.8.1, while according to JaCoCo changelog Kotlin 过滤器已在 0.8.2 中添加。
所以给出 src/main/kotlin/DataClass.kt
data class DataClass(var x)
src/test/kotlin/Tests.kt
class Tests {
@org.junit.Test
fun test_data_class() {
DataClass(42)
}
}
和build.gradle
buildscript {
ext.kotlin_version = "1.2.41"
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin"
apply plugin: "jacoco"
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile "junit:junit:4.12"
}
tasks["jacocoTestReport"].dependsOn("test")
使用 Gradle 4.7 执行 gradle jacocoTestReport
后,您会看到
加上
后
jacoco {
toolVersion = '0.8.2'
}
相同的Gradle和相同的命令将产生
P.S。我相信 exclude
你的尝试
test {
jacoco {
exclude("*equals", "*hashCode")
}
}
指的是exclusion of tests from execution, because jacoco
property of test
没有exclude
——作为对
的补充证明
test {
jacoco {
exclude("Tests.class")
}
}
上述示例导致零测试。
我正在尝试使用 Gradle 和 Kotlin 设置 JaCoCo,我的问题是我有很多 data class
es,它们具有编译器生成的 equals
、hashCode
和 toString
方法。
我在文档中读到,使用 JaCoCo 我可以使用 ignored 方法,但 JaCoCo 的 Gradle 插件似乎只支持 排除s。我该如何解决这个问题?
我试过这个:
test {
jacoco {
exclude("*equals", "*hashCode")
}
}
但我仍然可以在这些方法旁边看到 0%。
我做错了什么?
如JaCoCo version 0.8.1: the only thing you need to do - is to make sure that Gradle uses correct JaCoCo version. All filters implemented so far in JaCoCo are enabled unconditionally and take place during generation of report. In the same announcement and in announcement of 0.8.2公告中所述,您可以看到
With Gradle JaCoCo Plugin you can select both runtime and version for "JaCoCoReport" task using "toolVersion" - https://docs.gradle.org/current/userguide/jacoco_plugin.html
并且该默认版本取决于 Gradle 的版本 - 例如 Gradle 4.7 by default uses version JaCoCo 0.8.1, while according to JaCoCo changelog Kotlin 过滤器已在 0.8.2 中添加。
所以给出 src/main/kotlin/DataClass.kt
data class DataClass(var x)
src/test/kotlin/Tests.kt
class Tests {
@org.junit.Test
fun test_data_class() {
DataClass(42)
}
}
和build.gradle
buildscript {
ext.kotlin_version = "1.2.41"
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin"
apply plugin: "jacoco"
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile "junit:junit:4.12"
}
tasks["jacocoTestReport"].dependsOn("test")
使用 Gradle 4.7 执行 gradle jacocoTestReport
后,您会看到
加上
后jacoco {
toolVersion = '0.8.2'
}
相同的Gradle和相同的命令将产生
P.S。我相信 exclude
你的尝试
test { jacoco { exclude("*equals", "*hashCode") } }
指的是exclusion of tests from execution, because jacoco
property of test
没有exclude
——作为对
test {
jacoco {
exclude("Tests.class")
}
}
上述示例导致零测试。