cleanUp

suspend fun TestDsl.cleanUp(name: String, onSuccess: Boolean = true, onFailure: Boolean = true, block: suspend TestDsl.() -> Unit)(source)

Registers a block named name to run at the end of the test.

The block will run even if the test fails.

Finalizers are ran in inverse order as their registration order.

Example

val prepareDatabase by prepared { FakeDatabase() }

test("Create a user") {
val database = prepareDatabase()

val user = database.createUser()

cleanUp("Delete user $user") {
database.deleteUser(user)
}
}

Declaration from within prepared values

This function can be called from within a prepared value, in which case it will run when the test that initialized that prepared value finishes:

val prepareDatabase by prepared {
FakeDatabase()
.also { cleanUp("Disconnect from the database") { it.disconnect() } }
}

val prepareUser by prepared {
val database = prepareDatabase()
database.createUser()
.also { cleanUp("Delete user $it") { database.deleteUser(it) } }
}

test("Rename a user") {
val user = prepareUser()

user.rename("New name")

// will automatically run:
// 1. Delete user …
// 2. Disconnect from the database
}

Parameters

onSuccess

If false, this finalizer will not run if the test failed.

onFailure

If false, this finalizer will not run if the test was successful.