Kotest Configuration

Explains Kotest property testing configuration.

Configuration

Kotest provides a way to specify several configuration options when running property tests. This is done by passing a PropTestConfig instance to the test method.

For example:

class PropertyExample: StringSpec({
   "String size" {
      forAll<String, String>(PropTestConfig(options here...)) { a,b ->
         (a + b).length == a.length + b.length
      }
   }
})

Seed

A commonly used configuration option is the seed used for the random source. This option is used when you want the same values to be repeated every time the test runs. You can use this option after finding a test failure and wanting that specific set of values to continue running in the future as a regression test.

For more details on how to use seeds, click here.

Min Failure

By default, Kotest does not allow failures. You may want to run a non-deterministic test several times and accept a small number of failures. You can specify this in the configuration.

class PropertyExample: StringSpec({
   "some flakey test" {
      forAll<String, String>(PropTestConfig(maxFailure = 3)) { a,b ->
         // max of 3 inputs can fail
      }
   }
})

PropTestListener

In property tests, you may need to perform setup and teardown for each test iteration. To do this, you can register a PropTestListener in PropTestConfig.

class PropertyExample: StringSpec({
   "some property test which require setup and tear down in each iteration" {
      forAll<String, String>(PropTestConfig(listeners = listOf(MyPropTestListener))) { a,b ->
         // some assertion
      }
   }
})

References