Skip to main content
Version: 5.3

Closing resources automatically

You can let Kotest close resources automatically after all tests have been run:

class StringSpecExample : StringSpec() {

val reader = autoClose(StringReader("xyz"))

init {
"your test case" {
// use resource reader here
}
}
}

Resources that should be closed this way must implement java.lang.AutoCloseable. Closing is performed in reversed order of declaration after the return of the last spec interceptor.