Package kotlinx.coroutines.test

Types

Link copied to clipboard
interface DelayController

Control the virtual clock time of a CoroutineDispatcher.

Link copied to clipboard
class TestCoroutineDispatcher(val scheduler: TestCoroutineScheduler = TestCoroutineScheduler()) : TestDispatcher, Delay, SchedulerAsDelayController

CoroutineDispatcher that performs both immediate and lazy execution of coroutines in tests and uses a TestCoroutineScheduler to control its virtual clock.

Link copied to clipboard

An exception handler that captures uncaught exceptions in tests.

Link copied to clipboard

This is a scheduler for coroutines used in tests, providing the delay-skipping behavior.

Link copied to clipboard
interface TestCoroutineScope : CoroutineScope

A scope which provides detailed control over the execution of coroutines for tests.

Link copied to clipboard
abstract class TestDispatcher : CoroutineDispatcher, Delay

A test dispatcher that can interface with a TestCoroutineScheduler.

Link copied to clipboard
expect class TestResult

A test result.

actual typealias TestResult = Promise<Unit>
actual typealias TestResult = Unit
actual typealias TestResult = Unit
Link copied to clipboard
interface TestScope : CoroutineScope

A coroutine scope that for launching test coroutines.

Link copied to clipboard
interface UncaughtExceptionCaptor

Access uncaught coroutine exceptions captured during test execution.

Functions

Link copied to clipboard
fun TestScope.advanceTimeBy(delayTimeMillis: Long)

Moves the virtual clock of this dispatcher forward by the specified amount, running the scheduled tasks in the meantime.

fun TestCoroutineScope.advanceTimeBy(delayTimeMillis: Long)

Advances the testScheduler by delayTimeMillis and runs the tasks up to that moment (inclusive).

Link copied to clipboard
fun TestScope.advanceUntilIdle()

Advances the testScheduler to the point where there are no tasks remaining.

Advances the testScheduler to the point where there are no tasks remaining.

Link copied to clipboard
fun createTestCoroutineScope(context: CoroutineContext = EmptyCoroutineContext): TestCoroutineScope

A coroutine scope for launching test coroutines.

Link copied to clipboard
suspend fun TestCoroutineScope.pauseDispatcher(block: suspend () -> Unit)
Link copied to clipboard
fun Dispatchers.resetMain()

Resets state of the Dispatchers.Main to the original main dispatcher.

Link copied to clipboard
Link copied to clipboard
fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit)

Convenience method for calling runBlockingTest on an existing TestCoroutineDispatcher.

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

Executes a testBody inside an immediate execution dispatcher.

Link copied to clipboard
fun runBlockingTestOnTestScope(context: CoroutineContext = EmptyCoroutineContext, testBody: suspend TestScope.() -> Unit)

A version of runBlockingTest that works with TestScope.

Link copied to clipboard
fun TestScope.runCurrent()

Run any tasks that are pending at the current virtual time, according to the testScheduler.

Run any tasks that are pending at the current virtual time, according to the testScheduler.

Link copied to clipboard
fun TestScope.runTest(dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS, testBody: suspend TestScope.() -> Unit): TestResult

Performs runTest on an existing TestScope.

fun runTest(    context: CoroutineContext = EmptyCoroutineContext,     dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,     testBody: suspend TestScope.() -> Unit): TestResult

Executes testBody as a test in a new coroutine, returning TestResult.

fun TestCoroutineScope.runTest(dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS, block: suspend TestCoroutineScope.() -> Unit): TestResult

Runs a test in a TestCoroutineScope based on this one.

Link copied to clipboard
fun runTestWithLegacyScope(    context: CoroutineContext = EmptyCoroutineContext,     dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,     testBody: suspend TestCoroutineScope.() -> Unit): TestResult

This is an overload of runTest that works with TestCoroutineScope.

Link copied to clipboard
fun Dispatchers.setMain(dispatcher: CoroutineDispatcher)

Sets the given dispatcher as an underlying dispatcher of Dispatchers.Main. All subsequent usages of Dispatchers.Main will use the given dispatcher under the hood.

Link copied to clipboard
fun StandardTestDispatcher(scheduler: TestCoroutineScheduler? = null, name: String? = null): TestDispatcher

Creates an instance of a TestDispatcher whose tasks are run inside calls to the scheduler.

Link copied to clipboard
fun TestCoroutineScope(context: CoroutineContext = EmptyCoroutineContext): TestCoroutineScope

A coroutine scope for launching test coroutines using TestCoroutineDispatcher.

Link copied to clipboard
fun TestScope(context: CoroutineContext = EmptyCoroutineContext): TestScope

Creates a TestScope.

Link copied to clipboard
fun UnconfinedTestDispatcher(scheduler: TestCoroutineScheduler? = null, name: String? = null): TestDispatcher

Creates an instance of an unconfined TestDispatcher.

Properties

Link copied to clipboard
val TestScope.currentTime: Long

The current virtual time on testScheduler.

The current virtual time on testScheduler.

Link copied to clipboard
Link copied to clipboard
val TestCoroutineScope.uncaughtExceptions: List<Throwable>

List of uncaught coroutine exceptions, for backward compatibility.