testBlockingAndCancelScope

fun testBlockingAndCancelScope(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> Unit): TestResult

Runs block as a test and then cancels any coroutines it left running in the runner's scope, so a test that deliberately launches a never-completing child (e.g. an infinite collector) does not hang.

Returns a TestResult that the test framework awaits — it blocks until completion on JVM/Native and returns the backing Promise on js/wasmJs (which is single-threaded and cannot block). Use it the same way as testRunBlocking: @Test fun foo() = testBlockingAndCancelScope { … }.

Parameters

context

the context of the coroutine. The default value is an event loop on the current thread.

block

the test body.