【问题标题】:How can I run kotlintest tests with gradle?如何使用 gradle 运行 kotlintest 测试?
【发布时间】:2017-07-21 07:12:44
【问题描述】:

从 Intellij 启动时,kotlintest 测试运行得非常好,但是当我尝试使用 gradle test task 命令运行它们时,只能找到并运行我的常规 JUnit 测试。

kotlintest 代码:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.StringSpec

class HelloKotlinTest : StringSpec() {
    init {
        println("Start Kotlin UnitTest")

        "length should return size of string" {
            "hello".length shouldBe 5
        }
    }
}

build.gradle:

apply plugin: 'org.junit.platform.gradle.plugin'

buildscript {
    ext.kotlinVersion = '1.1.3'
    ext.junitPlatformVersion = '1.0.0-M4'

    repositories {
        maven { url 'http://nexus.acompany.ch/content/groups/public' }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
    }
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
    test.kotlin.srcDirs += 'test/main/kotlin'
}

(...) 

dependencies {
    // Kotlin
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jre8', version: kotlinVersion

    // Kotlin Test
    testCompile group: 'io.kotlintest', name: 'kotlintest', version: kotlinTestVersion

    // JUnit 5
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitJupiterVersion
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitJupiterVersion
}

【问题讨论】:

  • 你必须先解决如何运行junit5,我没有看到你添加junit5插件。
  • junit5 运行。我已将相关行添加到示例中。
  • 你有apply:'kotlin'吗?
  • 现在怎么样?

标签: junit kotlin build.gradle junit-runner kotlintest


【解决方案1】:

在 KotlinTest 3.1.x 中,您不再需要使用 Junit4。它与 JUnit 5 完全兼容。因此,您的问题的答案是升级到 3.1.x 轨道。

您需要将useJUnitPlatform() 添加到您的 build.gradle 中的测试块中。

您需要将testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9' 添加到您的依赖项中。

例如。

dependencies {
    testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9'
}

test {
    useJUnitPlatform()

    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = true

    testLogging {
        events "PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR"
    }
}

【讨论】:

    【解决方案2】:

    “解决方案”是切换回 JUnit 4。

    kotlintest 在构建时并未考虑到 JUnit 5,并且不提供自己的 junit 引擎。

    (注意:应该可以告诉 JUnit 5 使用 JUnit 4 引擎进行 kotlintest。如果有人知道如何做到这一点,请在此处添加解决方案。)

    【讨论】:

      【解决方案3】:

      要使用 Junit5 运行 kotlintest 测试,您需要使用 KTestRunner。

      @RunWith(KTestJUnitRunner::class)
      class MyTest : FunSpec({
          test("A test") {
              1 + 1 shouldBe 2
          }
      })
      

      以下面的 gradle 配置为例。

      dependencies {
          testCompile("io.kotlintest:kotlintest:${kotlinTestVersion}")
          testCompile("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
      }
      

      【讨论】:

      • 对我不起作用,使用 kotlintest 2.0.7 和 junit 5.1.0。我想同时想要 JUnit 5 和 kotlintest 是没有意义的。
      • 你是对的,到目前为止 TotlinTest 不支持 JUnit 5,他们仍然依赖 JUnit 4。
      • 实际上,几天以来,他们就这样做了,正如我在 his answer 中看到的那样,monkjack 指出了这一点。为我解决了问题!
      猜你喜欢
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 2015-04-07
      • 2017-03-28
      • 1970-01-01
      相关资源
      最近更新 更多