【问题标题】:Gradle does not find my tests with Kotlin and JUnit 5Gradle 没有找到我使用 Kotlin 和 JUnit 5 进行的测试
【发布时间】:2018-06-06 18:04:26
【问题描述】:

我已经用 Kotlin 和 Junit 5 编写了一些基本的单元测试。不幸的是,当我从 Intellij IDEA 运行它们时,Gradle 找不到这些测试。 我收到来自 Gradle 的消息“未找到给定的测试包括:[de.mhaug.phd.btcwallet.BasicTests]”和来自 Intellij 的消息“未收到测试事件”。

有趣的是,使用“./gradlew :clean :test”从命令行运行它会报告构建成功。但是,我的第一个测试显然是红色的,所以这表明 Gradle 没有执行它。

我已经尝试使用更详细的输出运行它,但没有任何帮助。这是一个最小(非)工作示例:

package de.mhaug.phd.btcwallet

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test

class BasicTests {
  @Test
  fun hey() {
    assertEquals(3,1+1)
  }

  @Test
  fun hey2() {
    assertFalse(3==1+1)
  }
}

这是我的 build.gradle:

buildscript {
    ext.kotlin_version = '1.2.10'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

group 'de.mhaug.phd'
version '1.0-SNAPSHOT'

apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.2'
    testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.2'
//    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: ''
//    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.2'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

如何让 Intellij/Gradle 执行我的测试?

【问题讨论】:

  • @JBNizet 谢谢。我的测试现在由 Gradle 执行。但是,Intellij 仍然显示消息“未找到测试事件”,尽管 est 在命令行中可见。
  • IntelliJ 可以直接执行 JUnit 测试 5。但是如果你将它配置为委托给 gradle,它就不能工作,因为它委托给标准的测试任务,而测试任务还不支持 JUnit 5。
  • 我正在使用 intellij 2020.2 并且在运行课程时运行良好,但我需要 gradle refresh(甚至可能打开 build.gradle.kts 以触发索引)以通过个别测试

标签: intellij-idea gradle junit kotlin


【解决方案1】:

不,@JB Nizet 的第一条评论指出了这一点:在您的 JUnit 5 依赖项旁边添加 test.useJUnitPlatform()。就是这样。

http://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle

test {
    useJUnitPlatform()
}

或:

dependency {
    ...
    // junit 5
    testImplementation ('org.junit.jupiter:junit-jupiter-api')
    testCompile ('org.junit.jupiter:junit-jupiter-params')
    testRuntime ('org.junit.jupiter:junit-jupiter-engine')
    testCompile ('org.mockito:mockito-junit-jupiter')
    test.useJUnitPlatform() // fix "test events not received" bug in IDEA
}

支持他的评论,而不是这个答案!

【讨论】:

  • 在使用 Kotlin 语法进行 gradle 时如何添加test.useJUnitPlatform()
  • 我不知道 kotlin,但这应该没关系,因为该行应该添加到 build.gradle 中,所以它也应该适用?
  • 在 Kotlin 中,我可以让它与 tasks.test { useJUnitPlatform() } 一起工作(在最外层范围内)。
  • 就我而言,我还必须在依赖项部分添加:testCompile 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
  • 另一个原因可能是您的test/itest 文件夹中的结构与main 中的结构不同。一旦我创建了一些集成测试并将它们放在src 下,但我忘记添加java 级别,如main;我只是输入了com.example.xxx,但所有测试都失败了。
【解决方案2】:

我认为您的测试未运行的特定问题已经或将通过将所有内容更新到最新版本(Gradle、Kotlin、IntelliJ 等)来解决。目前一切都在快速发展,因此必须保持最新状态。

关于“未收到测试事件”

据我所知,这是意料之中的。关键是,在使用 Gradle 任务时,要么相信你的测试已经运行,要么想办法显示每个测试的日志记录。

您需要区分“执行 gradle 任务”或“使用 IntelliJ 运行测试”(例如,通过单击测试旁边的装订线图标)。

从 IntelliJ 运行。

IntelliJ 与 Gradle 完美集成,从某种意义上说,如果您从 IntelliJ 运行,那么它不仅会运行测试(通过 Gradle?)而且还会显示一些不错的测试结果。 使用普通的 Gradle 或 Gradle Kotlin DSL 或任何你使用的任何东西都可以很好地与 JUnit 5、Kotlintest、Spek 配合使用。

无耻的自我宣传:JUnit 5 和 Gradle(也显示 Gradle 测试日志记录):How to use JUnit 5 with Gradle? 和 JUnit 5、Kotlintest 和 Gradle Kotlin DSL:How to run kotlintest tests using the Gradle Kotlin DSL?

运行 Gradle 任务。

但是,当您使用 Gradle 工具窗口运行 Gradle 任务时,您会看到您提到的“未收到测试事件”。我认为这是因为您只是运行了一个运行测试的 Gradle 任务,而这与 IntelliJ 本身没有任何关系,它没有集成它报告结果 - 它只是打印到命令行。

以运行 Kotlin 为例。当你使用 Gradle 任务时,它会在build/ 中输出。但是当您使用装订线图标运行 main 方法时,因此使用 IntelliJ 时,它将以out/ 输出。这是两种不同的运行方式。

【讨论】:

【解决方案3】:

我在使用 Java 11 和 JUnit 5 时遇到了同样的问题。我一公开课程,它就开始为我工作了。

【讨论】:

    【解决方案4】:

    我需要添加

            testImplementation(platform("org.junit:junit-bom:5.7.0"))
    

    也是。总之

            // junit 5
            testImplementation(platform("org.junit:junit-bom:5.7.0"))
            testImplementation('org.junit.jupiter:junit-jupiter-api')
            testCompile('org.junit.jupiter:junit-jupiter-params')
            testRuntime('org.junit.jupiter:junit-jupiter-engine')
            testCompile group: 'org.mockito', name: 'mockito-inline', version: '3.2.0'
            test.useJUnitPlatform() // fix "test events not received" bug in IDEA
            testCompile 'com.willowtreeapps.assertk:assertk-jvm:0.23'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-01
      • 2018-08-17
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2020-03-25
      • 2014-04-23
      相关资源
      最近更新 更多