How can I run kotlintest tests with gradle?

Viewed 9066

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.

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:

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
}
3 Answers

In order to run your kotlintest tests with Junit5 you need to use the KTestRunner.

@RunWith(KTestJUnitRunner::class)
class MyTest : FunSpec({
    test("A test") {
        1 + 1 shouldBe 2
    }
})

With the following gradle config for example.

dependencies {
    testCompile("io.kotlintest:kotlintest:${kotlinTestVersion}")
    testCompile("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
}
Related