我们刚刚将我们的单元测试移植到了JUnit5。意识到这仍然是相当早的采用,在google上几乎没有任何提示。
最具挑战性的是为我们在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'那么问题似乎是,在org.junit.platform.gradle.plugin中定义的junitPlatformTest在gradle生命周期阶段定义得太晚了,因此在解析脚本时是未知的。
为了仍然能够定义观察junitPlatformTest任务的jacoco任务,需要进行以下攻击。
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 task
}
// create junit platform jacoco task
project.task(type: JacocoReport, "junitPlatformJacocoReport",
{
sourceDirectories = files("./src/main")
classDirectories = files("$buildDir/classes/main")
executionData task
})
}
}最后,有必要配置junitPlatform插件。下面的代码允许运行junit 5标签的命令行配置:你可以通过运行以下命令来运行所有带有'unit‘标签的测试:
gradle clean junitPlatformTest -PincludeTags=unit您可以使用以下命令运行所有缺少unit和integ标记的测试
gradle clean junitPlatformTest -PexcludeTags=unit,integ如果未提供任何标记,则将运行所有测试(默认)。
junitPlatform {
engines {
include 'junit-jupiter'
include 'junit-vintage'
}
reportsDir = file("$buildDir/test-results")
tags {
if (project.hasProperty('includeTags')) {
for (String t : includeTags.split(',')) {
include t
}
}
if (project.hasProperty('excludeTags')) {
for (String t : excludeTags.split(',')) {
exclude t
}
}
}
enableStandardTestTask false
}发布于 2016-09-08 16:58:13
谢谢,所以黑客现在看起来是这样的:
project.afterEvaluate {
def junitPlatformTestTask = project.tasks.getByName('junitPlatformTest')
// configure jacoco to analyze the junitPlatformTest task
jacoco {
// this tool version is compatible with
toolVersion = "0.7.6.201602180812"
applyTo junitPlatformTestTask
}
// create junit platform jacoco task
project.task(type: JacocoReport, "junitPlatformJacocoReport",
{
sourceDirectories = files("./src/main")
classDirectories = files("$buildDir/classes/main")
executionData junitPlatformTestTask
})
}发布于 2017-07-04 21:54:33
也可以通过直接代理注入来解决:
subprojects {
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.9"
}
configurations {
testAgent {
transitive = false
}
}
dependencies {
testAgent("org.jacoco:org.jacoco.agent:0.7.9:runtime")
}
tasks.withType(JavaExec) {
if (it.name == 'junitPlatformTest') {
doFirst {
jvmArgs "-javaagent:${configurations.testAgent.singleFile}=destfile=${project.buildDir.name}/jacoco/test.exec"
}
}
}
}然后,报告将随jacocoTestReport任务一起提供
发布于 2016-09-07 20:22:35
要获得对junitPlatformTest任务的引用,另一种选择是在项目中实现一个afterEvaluate块,如下所示:
afterEvaluate {
def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
// do something with the junitPlatformTestTask
}有关更多示例,请参阅我在GitHub for JUnit 5上的评论。
https://stackoverflow.com/questions/39362955
复制相似问题