runTestWithMain

fun runTestWithMain(dispatcher: TestDispatcher = StandardTestDispatcher(), timeout: Duration? = null, testBody: suspend TestScope.() -> Unit): TestResult(source)

Runs testBody like runTest with dispatcher installed as Dispatchers.Main, and restores the platform's main dispatcher afterward, including when the body fails.

The test body and the code under test share dispatcher's scheduler, so advanceUntilIdle and virtual time cover work launched on Dispatchers.Main, such as a view model's scope.

@Test fun loads() = runTestWithMain {
val model = ProfileModel()
advanceUntilIdle()
assertEquals("Ready", model.state)
}

The platform dispatcher is restored once the returned TestResult has completed: when the call returns on the JVM, Android, and native targets, and when the promise settles on JS and Wasm. Return the result from the test function, as with runTest.

Return

The runTest result.

Parameters

dispatcher

The dispatcher installed as Dispatchers.Main and used to run the test.

timeout

The real-time limit for the body, or null for runTest's default.

testBody

The test, run in a TestScope that uses dispatcher.