junit - 如何使用 gradle 运行 kotlintest 测试?

标签 junit kotlin build.gradle junit-runner kotlintest

从 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
}

最佳答案

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

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

您需要将 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"
    }
}

关于junit - 如何使用 gradle 运行 kotlintest 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45231313/

相关文章:

kotlin - 不使用maven应用本地jar-plugin

java - 随机测试用例失败 - Forked JVM 异常退出。

android - Kotlin 按钮数组

java - 如何在 spring-boot 1.5.10 中升级 JUnit-5?

type-conversion - 转换容器类型?

android - 如何更改 Android Studio/Gradle 中所有模块的构建工具版本号?

Gradle 可选 @Input

java - Gradle:路径和 baseDir 都不能为 null 或空字符串

java - JUnit 重复注解不启动测试用例

java - 如何运行给定包的所有 JUnit 测试?