Skip to content

Execute with Kotest

Execute with Kotest

Execute Prepared tests alongside tests written using Kotest.

Prepared tests are declared using the helper preparedSuite.

class FooTest : StringSpec({
    // Declare tests normally using the Kotest syntax
    "Hello world" {
        "Hello world" shouldBe "Hello world"
    }

    // Declare tests using the Prepared syntax
    // using the 'preparedSuite' function
    preparedSuite {
        test("Hello world") {
            "Hello world" shouldBe "Hello world"
        }

        suite("A group of related tests") {
            test("Control the time") {
                println("Current time: ${time.nowMillis}ms")
            }

            test("Control randomness") {
                random.setSeed(1)

                println("Random value: ${random.nextInt()}")
            }
        }
    }
})

If you do not plan on mixing vanilla Kotest tests with your Prepared tests, you can also use the PreparedSpec:

class FooTest : PreparedSpec({
    test("Hello world") {
        "Hello world" shouldBe "Hello world"
    }

    suite("A group of related tests") {
        test("Control the time") {
            println("Current time: ${time.nowMillis}ms")
        }

        test("Control randomness") {
            random.setSeed(1)

            println("Random value: ${random.nextInt()}")
        }
    }
})

Configuration

JVM project

// build.gradle.kts

plugins {
    kotlin("jvm") version "…"
}

dependencies {
    implementation("dev.opensavvy.prepared:runner-kotest:…")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Multiplatform project

// build.gradle.kts

plugins {
    kotlin("multiplatform") version "…"

    // ⚠ Without the Kotest plugin, tests for some platforms 
    // may not run, without warnings ⚠
    // https://plugins.gradle.org/plugin/io.kotest.multiplatform
    id("io.kotest.multiplatform") version "…"
}

kotlin {
    jvm()
    js()
    // …

    sourceSets.commonTest.dependencies {
        implementation("dev.opensavvy.prepared:runner-kotest:…")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Features

This runner automatically enables some Kotest features:

Limitations

Kotest expects nested suites to be `suspend`. Because of this, Kotest cannot allow nested suites on Kotlin/JS. Prepared supports nested suites on all platforms; they are un-nested automatically when executing with Kotest.

Before using this runner, you may want to browse its planned work.

Packages

opensavvy.prepared.runner.kotest