我們僅將單元測試移植到JUnit5。意識到這仍然是相當早的採用與谷歌的一點提示。Gradle Jacoco和JUnit5
最具挑戰性的是讓jacoco代碼覆蓋我們使用上詹金斯的Junit5測試。由於這花了我近一天的時間才弄清楚,我以爲我分享了。儘管如此,如果你知道更好的解決方案,我會感興趣的!
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'
那麼問題似乎是,junitPlatformTest作爲org.junit.platform.gradle.plugin定義是指在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個標籤應運行: 可以通過運行運行與「單位」標籤的所有測試:
gradle clean junitPlatformTest -PincludeTags=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
}
我投票,因爲它是相當的解決方案,然後一個問題 – Stanislav
如果這是分成的問題,回答這將是很好,關閉這一問題作爲題外話。 –
我同意@PaulHicks這應該是一個自我回答的問題。你可以在這裏看到文檔(https://stackoverflow.com/help/self-answer)。此外,「Gradle生命週期中太遲」問題應該在[M5]中修復(https://github.com/junit-team/junit5/issues/708)。 – mkobit