Quick Start
Kotest is divided into several, stand alone, subprojects, each of which can be used independently:
You can decide to go all in on Kotest, and use all three together, or you can choose to one or more modules in conjunction with other projects. For example, you could use the assertions library with JUnit, or you could use the test framework with another assertions library like assertj.
This page gives setup instructions for various combinations of projects and targets.
Kotest is a multi-platform project.
If you are unfamiliar with this, then Kotlin compiles to different targets - JVM, JS, Native, iOS and so on. If you are doing server side or android development then you want the modules that end with JVM, such as kotest-property-jvm
.
Test Framework​
The Kotest test framework is supported on JVM, Javascript and Native. To enable Kotest for multiple platforms, combine the steps for the individual platforms as detailed in the following tabs.
- JVM/Gradle
- JVM/Maven
- Kotlin/JS
- Kotlin/Native
- Android
Kotest on the JVM uses the JUnit Platform gradle plugin.
For Gradle 4.6 and higher this is as simple as adding useJUnitPlatform()
inside the tasks with type Test
and then adding the Kotest junit5 runner dependency.
If you are using Gradle + Groovy then:
test {
useJUnitPlatform()
}
Or if you are using Gradle + Kotlin then:
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
And then the dependency:
testImplementation 'io.kotest:kotest-runner-junit5:$version'
A working multi-platform project with JVM, native and Javascript all configured, with unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples-multiplatform
Add the Kotest multiplatform gradle plugin to your build.
For example:
plugins {
id("io.kotest.multiplatform") version "5.0.2"
}
Add the engine dependency to your commonTest
dependencies block:
kotlin {
targets {
js(IR) { // LEGACY or BOTH are unsupported
browser() // to compile for the web
nodejs() // to compile against node
}
}
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:$version")
}
}
}
}
Only the new IR compiler backend for Kotlin/JS is supported. If you are compiling JS with the legacy compiler backend then you will not be able to use Kotest for testing.
Write your tests using FunSpec, ShouldSpec or StringSpec.
Tests can be placed in either commonTest
or jsTest
source sets. Run your tests using the gradle check
command.
The Javascript test engine is feature limited when compared to the JVM test engine. The major restriction is that annotation based configuration will not work as Kotlin does not expose annotations at runtime to javascript code.
Tests for Javascript cannot nest tests. This is due to the underlying Javascript test runners (such as Mocha or Karma)
not supporting promises in parent tests, which is incompatible with coroutines and in Kotest every test scope is a coroutine.
This is why the supported specs are limited to FunSpec
, ShouldSpec
and StringSpec
.
The IntelliJ Kotest plugin does not support running common, native or JS tests directly from the IDE using the green run icons. Only execution via gradle is supported.
A working multi-platform project with JVM, native and Javascript all configured, with unit and data driven test examples, can be found here: https://github.com/kotest/kotest-examples-multiplatform
Add the Kotest multiplatform gradle plugin to your build.
For example:
plugins {
id("io.kotest.multiplatform") version "5.0.2"
}
Add the engine dependency to your commonTest
dependencies block:
kotlin {
targets {
linuxX64() // can add any supported native targets such as linux, mac, windows etc
}
}
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:$version")
}
}
}
Tests can be placed in either commonTest
or a specific native sourceset.
Run your tests using the gradle check
command.
The native test engine is feature limited when compared to the JVM test engine. The major restriction is that annotation based configuration will not work as Kotlin does not expose annotations at runtime to native code.
The IntelliJ Kotest plugin does not support running common, native or JS tests from the IDE. You will need to use
the gradle check
task.
For maven you must configure the surefire plugin for junit tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
And then add the Kotest JUnit5 runner to your dependencies section.
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Currently, only JVM tests are officially supported in Kotest. Experimental support for instrumented and Robolectric tests is currently under work.
The following steps enable Kotest to be used for unit and integration tests, where the Android framework is not needed or is mocked that usually reside in the
src/test
folder of your module.
Kotest on Android uses the JUnit Platform gradle plugin. This requires configuring the android test options block in your build file and then adding the Kotest junit5 runner dependency.
android.testOptions {
unitTests.all {
useJUnitPlatform()
}
}
dependencies {
testImplementation 'io.kotest:kotest-runner-junit5:version'
}
To configure the test framework for both JS and JVM, you just combine copy the steps for JVM and JS.
Assertions Library​
The core assertions library framework is supported on all targets. Submodules are supported on the platforms that applicable. For example, the JDBC matchers only work for JVM since JDBC is a Java library.
- JVM/Gradle
- JVM/Maven
- Multiplatform
Add the following dependency to your build:
testImplementation 'io.kotest:kotest-assertions-core:$version'
Add the following dependency to your build.
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Add the following dependency to your commonTest dependencies block:
implementation 'io.kotest:kotest-assertions-core:$version'
Alternatively, add the dependency to a specific target. For example, we could add to the Javascript target only.
kotlin {
targets {
js {
browser()
nodejs()
}
}
sourceSets {
val jsTest by getting {
dependencies {
implementation("io.kotest:kotest-assertions-core:$version")
}
}
}
}
Property Testing​
The property test framework is supported on all targets.
- JVM/Gradle
- JVM/Maven
- Multiplatform
Add the following dependency to your build:
testImplementation 'io.kotest:kotest-property:$version'
Add the following dependency to your build.
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-property-jvm</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Add the following dependency to your commonTest dependencies block:
implementation 'io.kotest:kotest-property:$version'
Alternatively, add the dependency to a specific target. For example, we could add to the Javascript target only.
kotlin {
targets {
js {
browser()
nodejs()
}
}
sourceSets {
val jsTest by getting {
dependencies {
implementation("io.kotest:kotest-property:$version")
}
}
}
}
Snapshots​
Snapshot are automatically published on each commit to master.
If you want to test the latest snapshot build, setup the same way described above, change the version to the current snapshot version and add the following repository to your repositories
block:
https://oss.sonatype.org/content/repositories/snapshots