How to run kotlintest tests using the Gradle Kotlin DSL?

Since kotlintest version 3, JUnit 5 is supported! Just replace, in your build.gradle.kts,

testCompile("io.kotlintest:kotlintest:2.0.7")

by

testCompile("io.kotlintest:kotlintest-core:3.0.2")
testCompile("io.kotlintest:kotlintest-assertions:3.0.2")
testCompile("io.kotlintest:kotlintest-runner-junit5:3.0.2")

as clarified in the changelog.

Then either run the Gradle task check (in IntelliJ in the Gradle tool window on the right) or, in IntelliJ, click the gutter icon to run only a specific test or set of tests.


With more recent versions of the Kotlin Gradle plugin, adding this is sufficient:

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

tasks {
    test {
        useJUnitPlatform()
    }
}

This is assuming that you want to stick to the interface of kotlin.test. For example, a small test could look like this:

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

class SomeTest {
    @Test
    fun testBar() {
        assertEquals(5, 6)
    }
}

If you want to run your tests from IDEA, you need to add some additional dependencies:

dependencies {
    // ...
    testCompileOnly("org.junit.jupiter", "junit-jupiter-api", "5.5.2")
    testCompileOnly("org.junit.jupiter", "junit-jupiter-params", "5.5.2")
}

Similar to @PhPirate's answer, to use KotlinTest 3.0.x with gradle, you need to.

  1. Add the gradle plugin for junit 5 (also called the junit platform)
  2. Add the gradle junit plugin to the classpath of your buildscript
  3. Add KotlinTest junit5 runner to your test dependencies.

The first two steps are common for any project that uses junit platform - which includes junit5 itself, KotlinTest, Spek, and so on.

A basic gradle config looks like this:

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

You could also clone the gradle sample repo here which has a simple gradle project configured with KotlinTest.