C. *_*ber 19 gradle jenkins jacoco junit5
我们刚刚将单元测试移植到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 task
}
// create junit platform jacoco task
project.task(type: JacocoReport, "junitPlatformJacocoReport",
{
sourceDirectories = files("./src/main")
classDirectories = files("$buildDir/classes/main")
executionData task
})
}
}
Run Code Online (Sandbox Code Playgroud)
最后,有必要配置junitPlatform插件.以下代码允许运行junit 5标记的命令行配置:您可以通过运行以下命令运行带有'unit'标记的所有测试:
gradle clean junitPlatformTest -PincludeTags=unit
Run Code Online (Sandbox Code Playgroud)
您可以运行所有缺少单位和整数标记的测试
gradle clean junitPlatformTest -PexcludeTags=unit,integ
Run Code Online (Sandbox Code Playgroud)
如果没有提供标签,则将运行所有测试(默认).
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
}
Run Code Online (Sandbox Code Playgroud)
谢谢,所以黑客现在看起来像这样:
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
})
}
Run Code Online (Sandbox Code Playgroud)
要获取对任务的引用,另一种选择是在项目中junitPlatformTest
实现一个块,如下所示:afterEvaluate
afterEvaluate {
def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
// do something with the junitPlatformTestTask
}
Run Code Online (Sandbox Code Playgroud)
有关更多示例,请参阅我在GitHub 上对 JUnit 5 的评论。
归档时间: |
|
查看次数: |
4054 次 |
最近记录: |