如何使用Gradle运行kotlintest测试? [英] How can I run kotlintest tests with gradle?

查看:304
本文介绍了如何使用Gradle运行kotlintest测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Intellij启动时,kotlintest测试运行良好,但是当我尝试使用gradle test task命令运行它们时,只会发现并运行常规的JUnit测试.

The kotlintest tests run perfectly fine when started from Intellij, but when I try to run them with the gradle test task command, only my regular JUnit tests are found and run.

kotlintest代码:

The kotlintest code:

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:

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轨道.

In KotlinTest 3.1.x you don't need to use Junit4 anymore. It is fully compatible with JUnit 5. Thus the answer to your question is to upgrade to the 3.1.x track.

您需要在build.gradle中的测试块中添加useJUnitPlatform().

You need to add useJUnitPlatform() to the test block in your build.gradle.

您需要将testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9'添加到您的依赖项中.

You need to add testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9' to your dependencies.

例如.

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

这篇关于如何使用Gradle运行kotlintest测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆