繁体   English   中英

如何使用 Gradle Kotlin DSL 运行 kotlintest 测试?

[英]How to run kotlintest tests using the Gradle Kotlin DSL?

我想要什么?

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

有什么问题?

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

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

我尝试了什么?

如何使用 gradle 运行 kotlintest 测试? 本质上是问题的旧版本,但由于使用不同的技术而已经过时:JUnit 4 和 Gradle 而不是 Gradle Kotlin DSL。 我能找到的所有其他问题要么是针对 JUnit 4,要么是针对没有 kotlintest 的 JUnit 5

项目设置

我正在使用 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 上完成项目。

从 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")

更改日志中所述。

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

使用更新版本的 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")
}

与@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 示例存储库,其中包含一个使用 KotlinTest 配置的简单 gradle 项目。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM