[英]How to configure JUnit 5 in a Kotlin multiplatform project using Gradle and IntelliJ?
IntelliJ IDEA 2018.2.3(社區版)中的 Kotlin 多平台模板依賴於build.gradle
中的 JUnit 4.12 用於項目的 JVM 部分:
plugins {
id 'kotlin-platform-jvm' version '1.2.61'
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
expectedBy project(":TestMulti-common")
testCompile "junit:junit:4.12"
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
sourceCompatibility = "1.8"
使用這個模板,我可以將測試添加到項目的公共部分,並且測試被 IntelliJ 識別:“運行”圖標顯示在源文件的邊緣,並且測試可以通過上下文菜單運行。
如何使用 JUnit 5 實現類似的設置?
值得注意的是,我使用的是 Gradle 4.10(一些較舊的示例使用junit-platform-gradle-plugin
自 Gradle 4.6 以來已被棄用)。 關於如何設置的文檔已經過時且稀缺:
當我嘗試根據 JUnit 5 Gradle 示例為項目的 JVM 部分設置build.gradle
,我可以使用 Gradle 運行測試,但IntelliJ 似乎無法識別我的測試。
plugins {
id 'kotlin-platform-jvm' version '1.2.61'
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
expectedBy project(":TestMulti-common")
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit5"
testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
testCompile('org.junit.jupiter:junit-jupiter-params:5.3.1')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
sourceCompatibility = "1.8"
奇怪的是,在 JVM 項目上運行gradle test
時,有時測試結果會出現在 IntelliJ 的測試結果中,但是當重新運行測試時,會出現消息“未收到測試事件” 。 邊距中的“運行”圖標永遠不會出現在源文件中,上下文菜單中的測試選項也不會出現。
其他人似乎也有類似的問題,但尚不清楚這些問題是否相同/相關或已解決,因為:
許多不同的版本、過時的文檔和類似的問題使得很難找到有關此問題的更多信息。
這是 IntelliJ 中的一個錯誤,正如我在以下 YouTrack 問題中所述: IntelliJ 無法識別 Kotlin 多平台項目中的 JUnit 5 測試。
在最新版本中,這現在可以工作了。 我嘗試了 Kotlin 1.3.71、IntelliJ 插件 1.3.71-release-IJ2019.3-1 和 JUnit 5.6.0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.