Timeouts

Tests have two types of timeout that can be applied. The first is the overall time for all invocations of a test. This is just called timeout. The second is per individual run of a test, and this is called invocation timeout.

Kotest can be configured to invoke a test multiple times. For example,

class TimeoutTest : DescribeSpec({
describe("my test context") {
it("run me three times").config(invocations = 3) {
// some slow network test that takes 1500 millis
}
}
})

In this case, a timeout of 2000 millis would cause the test to fail, because the total run time would be 4500 millis. Whereas an invocation timeout of 2000 millis would not cause the test to fail, because each individual run is 1500 millis.

note

The time taken for a test includes the execution time taken for nested tests, so factor this into your timeouts.

We can specify the timeout at three levels.

Test Level Timeouts#

The most finely grained location for timeouts it on leaf tests directly.

class TimeoutTest : DescribeSpec({
describe("my test context") {
it("timeout after 750ms").config(timeout = 750.milliseconds, invocationTimeout = 250.milliseconds) {
}
}
})

Spec Level Timeouts#

Timeouts can be specified at the spec level for every test in that spec, unless overriden by the test case itself.

class TimeoutTest : DescribeSpec({
timeout = 1250.milliseconds
describe("I will timeout in 1250 millis") {
it("And so will I") { }
it("But I'm a little faster").config(timeout = 500.milliseconds) { }
}
})

Global Timeouts#

We can set global config in two ways - via system properties or by project config.

System Property#

To set the global timeout or invocation timeout at the command line, use the system property kotest.framework.timeout and kotest.framework.invocation.timeout with a value in milliseconds.

Project Config#

We can set a global default for both timeout and invocationTimeout inside project config.

object ProjectConfig : AbstractProjectConfig {
override val timeout = 2.seconds
override val invocationTimeout = 1.second
}

Global config is overridden by spec level and test case level values.