Kotest Seeds

When a property test runs, values are generated using a random source created from a seed value.

Property Test Seeds

When a property test runs, values are generated using a random source created from a seed value. By default, this seed value is chosen randomly, using the default kotlin.random.Random instance. However, there are cases where you may want to fix or repeat this value.

To change the default used by all tests unless overridden by the options listed below, change the configuration value PropertyTesting.defaultSeed.

Specifying a Seed Manually

To set a seed manually, pass an instance of PropTestConfig to the property test method. You might do this after finding a test failure and wanting that value to keep running as a regression test in the future.

For example:

import io.kotest.core.spec.style.StringSpec
import io.kotest.property.PropTestConfig
import io.kotest.property.forAll

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

Re-running Failed Seeds

By default, when a property test fails, the seed used by that test is written to a file under ~/.kotest/seeds/<spec>/<testname>. Whenever the property test runs, Kotest detects whether this file exists and uses this seed instead of a random seed. The next time the test succeeds, the seed file is removed.

Failing When a Seed Is Set

Some users may not want to specify seeds manually. They may want to use them only locally during development and not check them in. In that case, set PropertyTesting.failOnSeed = false on the server or set the environment variable kotest.proptest.seed.fail-if-set to false.

Then, if a seed is detected, the test suite fails.


References