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

2024-04-26

我想要什么?

我想使用运行我的测试科特林测试 https://github.com/kotlintest/kotlintest,我通过单击测试类旁边的图标成功从 IntelliJ 运行它们。 我也有JUnit 5 https://junit.org/junit5/在我的项目中进行测试。 我现在开始使用Gradle Kotlin DSL https://github.com/gradle/kotlin-dsl,我成功运行了 Gradle 任务check它执行 JUnit 5 任务。

问题是什么?

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

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

我尝试了什么?

如何使用 gradle 运行 kotlintest 测试? https://stackoverflow.com/questions/45231313/how-can-i-run-kotlintest-tests-with-gradle/48459420?noredirect=1#comment86266799_48459420本质上是问题的旧版本,但由于使用不同的技术而已经过时:JUnit 4 和 Gradle 而不是 Gradle Kotlin DSL。 我能找到的所有其他问题都是针对 JUnit 4 的,或者没有 kotlintest 的 JUnit 5 https://stackoverflow.com/a/49014534/4126843.

项目设置

我正在使用 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)
    }

}

and my build.gradle.kts is

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 https://github.com/PHPirates/kotlin-template-project.


对于最新版本的 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")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Gradle Kotlin DSL 运行 kotlintest 测试? 的相关文章

随机推荐