runBlockingTest

@ExperimentalCoroutinesApi fun runBlockingTest(
    context: CoroutineContext = EmptyCoroutineContext,
    testBody: suspend TestCoroutineScope.() -> Unit
): 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.

Exceptions

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

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.

@ExperimentalCoroutinesApi fun TestCoroutineScope.runBlockingTest(
    block: suspend TestCoroutineScope.() -> Unit
): <ERROR CLASS>

Convenience method for calling runBlockingTest on an existing TestCoroutineScope.

@ExperimentalCoroutinesApi fun TestCoroutineDispatcher.runBlockingTest(
    block: suspend TestCoroutineScope.() -> Unit
): <ERROR CLASS>

Convenience method for calling runBlockingTest on an existing TestCoroutineDispatcher.