[英]How to configure JUnit 5 in a Kotlin multiplatform project using Gradle and IntelliJ?
The Kotlin multiplatform template in IntelliJ IDEA 2018.2.3 (Community Edition) relies on JUnit 4.12 in build.gradle
for the JVM part of the project: 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"
Using this template, I can add tests to the common part of the project and the tests are recognized by IntelliJ : a 'Run' icon shows up in the margin of the source files and tests can be run through the context menu.使用这个模板,我可以将测试添加到项目的公共部分,并且测试被 IntelliJ 识别:“运行”图标显示在源文件的边缘,并且测试可以通过上下文菜单运行。
How can I achieve a similar setup using JUnit 5?如何使用 JUnit 5 实现类似的设置?
Of note, I am using Gradle 4.10 (some older examples use junit-platform-gradle-plugin
which has been deprecated since Gradle 4.6 ).值得注意的是,我使用的是 Gradle 4.10(一些较旧的示例使用
junit-platform-gradle-plugin
自 Gradle 4.6 以来已被弃用)。 Documentation on how to set this up is outdated and scarce:关于如何设置的文档已经过时且稀缺:
When I try to set up the build.gradle
for the JVM part of the project based on the JUnit 5 Gradle example , I can run tests using Gradle , but IntelliJ does not seem to recognize my tests .当我尝试根据 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"
Oddly enough, sometimes test results appear in IntelliJ's test results when running gradle test
on the JVM project, but when rerunning the tests the message "Test events were not received" shows up.奇怪的是,在 JVM 项目上运行
gradle test
时,有时测试结果会出现在 IntelliJ 的测试结果中,但是当重新运行测试时,会出现消息“未收到测试事件” 。 The 'Run' icons in the margin never appear in source files and neither do the test options in the context menu.边距中的“运行”图标永远不会出现在源文件中,上下文菜单中的测试选项也不会出现。
Other people seem to have similar issues, but it is unclear whether or not these are the same/related or have been resolved since:其他人似乎也有类似的问题,但尚不清楚这些问题是否相同/相关或已解决,因为:
The many different releases, outdated documentation, and similar issues make it hard to find more information about this problem.许多不同的版本、过时的文档和类似的问题使得很难找到有关此问题的更多信息。
This was a bug in IntelliJ as I described in the following YouTrack issue: IntelliJ does not recognize JUnit 5 tests in Kotlin multiplatform project .这是 IntelliJ 中的一个错误,正如我在以下 YouTrack 问题中所述: IntelliJ 无法识别 Kotlin 多平台项目中的 JUnit 5 测试。
In the latest version this is now working.在最新版本中,这现在可以工作了。 I tried with Kotlin 1.3.71, the IntelliJ plugin 1.3.71-release-IJ2019.3-1, and JUnit 5.6.0.
我尝试了 Kotlin 1.3.71、IntelliJ 插件 1.3.71-release-IJ2019.3-1 和 JUnit 5.6.0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.