简体   繁体   English

如何使用 Gradle 和 IntelliJ 在 Kotlin 多平台项目中配置 JUnit 5?

[英]How to configure JUnit 5 in a Kotlin multiplatform project using Gradle and IntelliJ?

JUnit 4 (working) JUnit 4(工作)

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 识别:“运行”图标显示在源文件的边缘,并且测试可以通过上下文菜单运行。

JUnit 5 (tests not properly recognized in IntelliJ) JUnit 5(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。

In my case IntelliJ IDEA upgrade disabled JUnit plugin, so I needed to enable it again (requires IDE restart):在我的情况下,IntelliJ IDEA 升级禁用了 JUnit 插件,所以我需要再次启用它(需要重新启动 IDE):

需要启用 IntelliJ IDEA JUNit 插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ Kotlin多平台项目Gradle同步时间很长 - IntelliJ Kotlin multiplatform project Gradle sync very long 使用 intellij、kotlin 多平台和 gradle 时找不到 Android 平台 - Android platform not found when using intellij, kotlin multiplatform and gradle 如何在多平台 kotlin / native intellij 项目中添加依赖项? - How to add a dependency in a multiplatform kotlin / native intellij project? 在Kotlin多平台上使用Gradle阴影 - Using gradle shadow with Kotlin multiplatform 如何在 Kotlin/多平台项目中使用 Gradle liquibaseRuntime 配置 - How to use Gradle liquibaseRuntime configuration in a Kotlin/Multiplatform project 如何在 build.gradle.kts 中设置 gradle 任务以在 Kotlin(1.4) 多平台项目中创建 fatJar - how to set up a gradle task to create fatJar in Kotlin(1.4) Multiplatform project in build.gradle.kts 我可以在 Gradle 插件中配置 Kotlin 多平台模块吗? - Can I configure a Kotlin Multiplatform module in a Gradle plugin? 如何在多平台 Android 模块中配置 Kotlin jvmTarget? - How to configure Kotlin jvmTarget in a Multiplatform Android module? 为 Kotlin 多平台项目设置 gradle 和项目结构 - Setting up gradle and project structure for Kotlin Multiplatform project 如何降级 kotlin 多平台项目上的 gradle 以使用 Android Studio 稳定版? - How can i downgrade the gradle on project kotlin multiplatform to use a Android Studio Stable version?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM