Kotest Support

Konsist + Kotest

Konsist has first-class support for Kotest meaning that every following release will be developed with Kotest compatibility in mind. API has been improved to support Kotest flows. However, Konsist cannot automatically retrieve Kotest test names, meaning the test name won't appear in error logs upon test failure. To fully utilize Konsist with Kotest, you must explicitly provide the test name.

Setting The Test Name

Konsist can't obtain the test name from all dynamic tests (including Kotest tests).

It's recommended to provide the test name using the testName parameter. Supplying a test name provides additional benefits:

  • The appropriate test names will appear in the log if the test fails.

  • Test suppression will be facilitated (See Suppress Konsist Test)

See Explicit Test Names for more details.

Kotest enables fetching the test name from the context to populate the testName argument, ensuring consistent naming of tests:

class UseCaseTest : FreeSpec({
    "useCase test" {
        Konsist
            .scopeFromProject()
            .classes()
            .assertTrue (testName = this.testCase.name.testName) {  }
    }
})

This example is used FreeSpec however Kotest provides multiple testing styles.

KoTestName Extension

To facilitate test name retrieval you can add a custom koTestName extension:

val TestScope.koTestName: String
    get() = this.testCase.name.testName

This extension enables more concise syntax for providing Kotest test name:

class UseCaseTest : FreeSpec({
    "useCase test" {
        Konsist
            .scopeFromProject()
            .classes()
            .assertTrue (testName = koTestName) {  } // extension used
    }
})

The above test will execute multiple assertions per test (all use cases will be verified in a single test). If you prefer better isolation and more visibility you can execute every assertion as a separate test. See theDynamic Konsist Tests page.

Last updated