runBlockingTest

fun runBlockingTest(context: CoroutineContext = EmptyCoroutineContext, testBody: suspend TestCoroutineScope.() -> Unit)

Executes a testBody inside an immediate execution dispatcher.

This is similar to runBlocking but it will immediately progress past delays and into launch and async blocks. You can use this to write tests that execute in the presence of calls to delay without causing your test to take extra time.

@Test
fun exampleTest() = runBlockingTest {
val deferred = async {
delay(1_000)
async {
delay(1_000)
}.await()
}

deferred.await() // result available immediately
}

This method requires that all coroutines launched inside testBody complete, or are cancelled, as part of the test conditions.

Unhandled exceptions thrown by coroutines in the test will be re-thrown at the end of the test.

Parameters

context

additional context elements. If context contains CoroutineDispatcher or CoroutineExceptionHandler, then they must implement DelayController and TestCoroutineExceptionHandler respectively.

testBody

The code of the unit-test.

Throws

If the testBody does not complete (or cancel) all coroutines that it launches (including coroutines suspended on join/await).


fun TestCoroutineScope.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit)

Convenience method for calling runBlockingTest on an existing TestCoroutineScope.


fun TestScope.runBlockingTest(block: suspend TestScope.() -> Unit)

Convenience method for calling runBlockingTestOnTestScope on an existing TestScope.


fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit)

Convenience method for calling runBlockingTest on an existing TestCoroutineDispatcher.