【发布时间】:2018-05-18 09:32:46
【问题描述】:
我们希望将 Gradle 与 Kotlin 和 Jacoco (+JUnit 5) 结合使用来生成代码覆盖率报告。
我们的项目目录树如下:
project/{src,test}/main/kotlin ...
我们的 build.gradle 文件如下所示:
jacoco {
toolVersion = "0.7.9"
reportsDir = file("$buildDir/reports")
applyTo junitPlatformTest
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage report."
classDirectories = fileTree(
dir: "$buildDir/classes/kotlin/main"
)
def coverageSourceDirs = [
"src/main/kotlin"
]
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/jacoco/junitPlatformTest.exec")
reports {
xml.enabled = true
html.enabled = true
csv.enabled = true
}
}
test {
jacoco {
append = false
destinationFile = file("$buildDir/jacoco/junitPlatformTest.exec")
includeNoLocationClasses = true
}
}
test.dependsOn junitPlatformTest
通过这种配置,Jacoco 会生成一个 html 报告并将其放在build/reports/test/html 下。但是,它显示我的覆盖率为 0%。这不应该是这样,因为我有一个测试用例为项目中的单个虚拟类执行所有方法。
我浏览了几个帖子,例如:
- http://vgaidarji.me/blog/2017/12/20/how-to-configure-jacoco-for-kotlin-and-java-project/
- Jacoco is reporting 0 coverage of Kotlin classes by unit tests, in an Android project
- JaCoCo returning 0% Coverage with Kotlin and Android 3.0
但是,我还没有找到适合我的解决方案。
【问题讨论】:
-
这很奇怪......在将我的 JUnit 测试从 TestXY 重命名为 XYTest 之后,它似乎工作了。我一定错过了一些关于命名约定的限制。
-
junit-platform-gradle-plugin使用标准包含模式,除非明确配置:junit.org/junit5/docs/current/api/org/junit/platform/engine/… -
非常感谢马克!我错过了文档中的这一点。
-
这个链接帮助了我:kevcodez.de/index.php/2018/08/…
-
Marcin Stachniuk 提到的 URL 现在是 kevcodez.de/posts/…
标签: gradle kotlin jacoco junit5