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

PHP*_*ate 6 gradle kotlin junit5 kotlintest gradle-kotlin-dsl

我想要什么?

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

问题是什么?

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

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

我尝试了什么?

如何使用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)
    }

}
Run Code Online (Sandbox Code Playgroud)

我的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()
}
Run Code Online (Sandbox Code Playgroud)

PS GitHub上的 Complete项目。

Rap*_*ael 6

对于最新版本的 Kotlin Gradle 插件,添加以下内容就足够了:

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

tasks {
    test {
        useJUnitPlatform()
    }
}
Run Code Online (Sandbox Code Playgroud)

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

import kotlin.test.Test
import kotlin.test.assertEquals

class SomeTest {
    @Test
    fun testBar() {
        assertEquals(5, 6)
    }
}
Run Code Online (Sandbox Code Playgroud)

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

dependencies {
    // ...
    testCompileOnly("org.junit.jupiter", "junit-jupiter-api", "5.5.2")
    testCompileOnly("org.junit.jupiter", "junit-jupiter-params", "5.5.2")
}
Run Code Online (Sandbox Code Playgroud)


mon*_*ack 3

与 @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'
}
Run Code Online (Sandbox Code Playgroud)

您还可以在此处克隆gradle 示例存储库,其中有一个使用 KotlinTest 配置的简单 gradle 项目。