JUnit 4(正在运行)

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"

使用此模板,我可以将测试添加到项目的公共(public)部分,并且IntelliJ可以识别测试:源文件的空白处会显示一个“运行”图标,并且可以通过上下文菜单运行测试。

JUnit 5(IntelliJ中无法正确识别的测试)

如何使用JUnit 5实现类似的设置?

值得注意的是,我正在使用Gradle 4.10(一些较旧的示例使用 junit-platform-gradle-plugin has been deprecated since Gradle 4.6)。有关如何进行设置的文档过时且稀缺:
  • 如上所示,IntelliJ中内置的默认项目模板不使用JUnit5。
  • JUnit团队provides examples, but not for Kotlin multiplatform

  • 当我尝试为项目based on the JUnit 5 Gradle example的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的测试结果中,但是当重新运行测试时,会显示“未收到测试事件”消息。页边空白中的“运行”图标永远不会出现在源文件中,上下文菜单中的测试选项也不会出现。

    其他人似乎也有类似的问题,但是尚不清楚这些问题是否相同/相关或已解决,因为:
  • Junit 5 and Gradle 4.6 check task shows "Test events were not received"
  • junit5 tests are not shown in test view
  • No "Run" icon on the left for tests in common part of a multi-platform project
  • Kotlin multiplatform projects run common module test in IDEA

  • 许多不同的发行版,过时的文档以及类似的问题使得很难找到有关此问题的更多信息。

    最佳答案

    正如我在以下YouTrack问题IntelliJ does not recognize JUnit 5 tests in Kotlin multiplatform project中所述,这是IntelliJ中的错误。

    在最新版本中,此功能现在可以使用。我尝试使用Kotlin 1.3.71,IntelliJ插件1.3.71-release-IJ2019.3-1和JUnit 5.6.0。

    08-05 19:07