我们刚刚将单元测试移植到JUnit5.意识到这仍然是相当早期的采用,在谷歌上几乎没有提示.
最具挑战性的是获得我们在jenkins上使用的Junit5测试的jacoco代码覆盖率.因为这花了我差不多一天才弄明白,我想我分享了.不过,如果您知道更好的解决方案,我很有兴趣知道!
buildscript {
dependencies {
// dependency needed to run junit 5 tests
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
}
}
// include the jacoco plugin
plugins {
id 'jacoco'
}
dependencies {
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
runtime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
runtime "org.junit.vintage:junit-vintage-engine:4.12.0-M2"
}
apply plugin: 'org.junit.platform.gradle.plugin'
Run Code Online (Sandbox Code Playgroud)
然后问题似乎是org.junit.platform.gradle.plugin中定义的junitPlatformTest在gradle生命周期阶段定义太晚,因此在解析脚本时是未知的.
为了仍然能够定义观察junitPlatformTest任务的jacoco任务,需要以下hack.
tasks.whenTaskAdded { task ->
if (task.name.equals('junitPlatformTest')) {
System.out.println("ADDING TASK " + task.getName() + " to the project!")
// configure jacoco to analyze the junitPlatformTest task
jacoco {
// this tool version is compatible with
toolVersion = "0.7.6.201602180812"
applyTo …Run Code Online (Sandbox Code Playgroud)