Skip to content

Suiteopensavvy.prepared.suite.randomrandom

random


Random generator control center.

Why?

We often need randomness in tests, for example to generate test data, or for property testing. However, randomness hurts reproducibility.

When a random value is generated using this helper, the random generator's seed is printed to the test's standard output. If we want to reproduce a previous test execution (e.g. rerun a failed CI test locally), we can add a call to setSeed at the start of the test.

Thread-safety

The Random class from the Kotlin standard library is not thread-safe. Since Prepared tests are encouraged to be asynchronous, this would risk making reproducibility impossible.

For this reason, Prepared exposes the traditional nextInt-style functions, which follow the same signature as the Random class, but hidden behind a lock.

test("A test that uses random values") {
    val int = random.nextInt()

    println("Generated integer: $int")
}

If you really need to access the underlying Random class, see Random.accessUnsafe.

Prepared values

When writing tests with Prepared, we often prefer to declare values used in the tests before the test declaration itself, using prepared values. However, the functions mentioned above are only available on this helper, which only exists in tests.

To simplify this pattern, we also expose randomInt and similar functions which expose prepared providers.

val int by randomInt()

test("A test that uses random values") {
    println("Generated integer: ${int()}")
}

As an added benefit, since prepared values log their actual result, all random values generated this way log themselves.