Kotest Support

Konsist + Kotest

Konsist has first-class support for Kotest meaning that every following release will be developed with Kotets compatibility in mind. API has been improved to support Kotst flows.

At the moment there is an additional step that is required for Konsist Kotest to be fully functional - the test name has to be explicitly provided.

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 Namesfor 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

In the future, this extension will be added to the Konsist.

This extension enables more concise syntax to provide Kotest test name:

class UseCaseTest : FreeSpec({
    "useCase test" {
        Konsist
            .scopeFromProject()
            .classes()
            .assertTrue (testName = koTestNamee) {  } // 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