How can I run kotlintest tests with gradle?
Asked Answered
C

3

7

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
}
Consueloconsuetude answered 21/7, 2017 at 7:12 Comment(4)
you must solve how to run junit5 first, I don't see you add junit5 plugin .Leavening
junit5 runs. I've added the relevant lines to the example.Consueloconsuetude
have you apply:'kotlin'?Leavening
how about it now?Leavening
S
11

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.

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

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

For example.

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"
    }
}
Selfdiscipline answered 29/3, 2018 at 13:41 Comment(0)
C
1

The "solution" was to switch back to JUnit 4.

kotlintest was not build with JUnit 5 in mind and does not provide its own junit-engine.

(Note: It should be possible to tell JUnit 5 to use the JUnit 4 engine for kotlintest. If anybody knows how to do this, please add the solution here.)

Consueloconsuetude answered 24/7, 2017 at 11:38 Comment(0)
F
1

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}")
}
Forelady answered 26/1, 2018 at 10:19 Comment(3)
Didn't work for me, using kotlintest 2.0.7 and junit 5.1.0. I guess it doesn't make sense to want both JUnit 5 and kotlintest.Levin
You are right, so far TotlinTest do not support JUnit 5, they still rely on JUnit 4.Forelady
Actually now, since a couple of days, they do, as I see monkjack pointed out in his answer. Solved the problem for me!Levin

© 2022 - 2024 — McMap. All rights reserved.