【问题标题】:How to run kotlintest tests using the Gradle Kotlin DSL?如何使用 Gradle Kotlin DSL 运行 kotlintest 测试?
【发布时间】:2018-09-13 06:36:04
【问题描述】:

我想要什么?

我想使用kotlintest 运行我的测试,我通过单击测试类旁边的图标成功地从 IntelliJ 运行它们。 我的项目中也有JUnit 5 测试。 我现在开始使用Gradle Kotlin DSL,并且我设法运行了执行JUnit 5 任务的Gradle 任务check

有什么问题?

kotlintest 测试未运行! Gradle 似乎没有发现它,因为失败的测试让 Gradle 任务成功。所以,

如何在使用 JUnit 5 的同时使用 Gradle Kotlin DSL 运行 kotlintest 测试?

我尝试了什么?

How can I run kotlintest tests with gradle? 本质上是问题的旧版本,但由于使用不同的技术已经过时:JUnit 4 和 Gradle 而不是 Gradle Kotlin DSL。 我能找到的所有其他问题都是针对 JUnit 4 或 JUnit 5 without kotlintest

项目设置

我正在使用 Gradle 4.6。 我的测试是

import io.kotlintest.matchers.exactly
import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.FunSpec

class KotlinTest: FunSpec() {

    init {
        testCalculate()
    }

    /** This failing test will not fail the Gradle check. */
    fun testCalculate() = test("one plus one is two") {
        (1+1).toDouble() shouldBe exactly(42.0)
    }

}

而我的build.gradle.kts

import org.gradle.api.plugins.ExtensionAware

import org.junit.platform.gradle.plugin.FiltersExtension
import org.junit.platform.gradle.plugin.EnginesExtension
import org.junit.platform.gradle.plugin.JUnitPlatformExtension

group = "mypackage"
version = "0.0"

// JUnit 5
buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.junit.platform:junit-platform-gradle-plugin:1.1.0")
    }
}

apply {
    plugin("org.junit.platform.gradle.plugin")
}

// Kotlin configuration.
plugins {
    application
    kotlin("jvm") version "1.2.30"
    java // Required by at least JUnit.
}

application {
    mainClassName = "mypackage.HelloWorld"
}

dependencies {
    compile(kotlin("stdlib"))
    // To "prevent strange errors".
    compile(kotlin("reflect"))
    // Kotlin reflection.
    compile(kotlin("test"))
    compile(kotlin("test-junit"))

    // JUnit 5
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.0")
    testRuntime("org.junit.platform:junit-platform-console:1.1.0")

    // Kotlintests are not run anyway when using JUnit 5 as well.
    testCompile("io.kotlintest:kotlintest:2.0.7")

}

repositories {
    jcenter()
}

PS 在GitHub 完成项目。

【问题讨论】:

    标签: gradle kotlin junit5 gradle-kotlin-dsl kotlintest


    【解决方案1】:

    从 kotlintest 版本 3 开始,支持 JUnit 5!只需替换,在您的build.gradle.kts

    testCompile("io.kotlintest:kotlintest:2.0.7")
    

    通过

    testCompile("io.kotlintest:kotlintest-core:3.0.2")
    testCompile("io.kotlintest:kotlintest-assertions:3.0.2")
    testCompile("io.kotlintest:kotlintest-runner-junit5:3.0.2")
    

    changelog 中所述。

    然后运行 ​​Gradle 任务 check(在 IntelliJ 中右侧 Gradle 工具窗口中),或者在 IntelliJ 中,单击装订线图标以仅运行特定测试或一组测试。

    【讨论】:

    • 这会将测试设置为运行,还是您需要更改测试配置以使其运行?
    • @jrtapsell “测试配置”是什么意思?如果您的 build.gradle.kts 中有正确的依赖项,那么执行 gradle 任务 check 应该会运行您的测试。
    • 我认为您在最初的帖子中唯一做错的事情是您没有将 KotlinTest 导入更改为 io.kotlintest:kotlintest-runner-junit5 而不是 io.kotlintest:kotlintest
    • @monkjack 抱歉,您指的是哪些进口产品?我找不到应该在哪里导入io.kotlintest:kotlintest。顺便说一句,谢谢你的回答,它有一个更简单的例子。我希望它能让更多人更容易使用 Gradle Kotlin DSL! (就我个人而言,我发现这些文档对于初学者来说有点欠缺......)
    【解决方案2】:

    与@PhPirate 的回答类似,要将 KotlinTest 3.0.x 与 gradle 结合使用,您需要这样做。

    1. 为 junit 5(也称为 junit 平台)添加 gradle 插件
    2. 将 gradle junit 插件添加到构建脚本的类路径中
    3. 将 KotlinTest junit5 运行器添加到您的测试依赖项中。

    前两个步骤对于使用 junit 平台的任何项目都很常见 - 包括 junit5 本身、KotlinTest、Spek 等。

    基本的 gradle 配置如下所示:

    apply plugin: 'org.junit.platform.gradle.plugin'
    
    buildscript {
        ext.kotlin_version = '1.2.31'
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.0"
        }
    }
    
    dependencies {
        testCompile 'io.kotlintest:kotlintest-runner-junit5:3.0.2'
    }
    

    您还可以在此处克隆 gradle sample repo,它有一个使用 KotlinTest 配置的简单 gradle 项目。

    【讨论】:

      【解决方案3】:

      对于更新版本的 Kotlin Gradle 插件,添加它就足够了:

      dependencies {
          testImplementation(kotlin("test-junit5"))
          testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.5.2")
      }
      
      tasks {
          test {
              useJUnitPlatform()
          }
      }
      

      这是假设你想坚持kotlin.test的界面。 例如,一个小测试可能如下所示:

      import kotlin.test.Test
      import kotlin.test.assertEquals
      
      class SomeTest {
          @Test
          fun testBar() {
              assertEquals(5, 6)
          }
      }
      

      如果你想从 IDEA 运行你的测试,你需要添加一些额外的依赖:

      dependencies {
          // ...
          testCompileOnly("org.junit.jupiter", "junit-jupiter-api", "5.5.2")
          testCompileOnly("org.junit.jupiter", "junit-jupiter-params", "5.5.2")
      }
      

      【讨论】:

      • 你的意思是用testImplementation(kotlin("test-junit5"))替换kotlintest依赖?对于问题中的测试代码,这对我来说真的不起作用,我得到了未解决的引用。您能否指出有关此的文档,或提供完整的build.gradle.kts
      • @PHPirate 我的错,我没有意识到你不会选择kotlin.test
      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2019-04-06
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多