Package kotlinx.coroutines.test

Types

DelayController
Link copied to clipboard
interface DelayController

Control the virtual clock time of a CoroutineDispatcher.

TestCoroutineDispatcher
Link copied to clipboard

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

TestCoroutineExceptionHandler
Link copied to clipboard

An exception handler that captures uncaught exceptions in tests.

TestCoroutineScheduler
Link copied to clipboard

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

TestCoroutineScope
Link copied to clipboard
interface TestCoroutineScope : CoroutineScope

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

TestDispatcher
Link copied to clipboard
abstract class TestDispatcher : CoroutineDispatcher, Delay

A test dispatcher that can interface with a TestCoroutineScheduler.

TestResult
Link copied to clipboard
expect class TestResult

A test result.

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

A coroutine scope that for launching test coroutines.

UncaughtExceptionCaptor
Link copied to clipboard
interface UncaughtExceptionCaptor

Access uncaught coroutine exceptions captured during test execution.

Functions

advanceTimeBy
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).

advanceUntilIdle
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.

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

A coroutine scope for launching test coroutines.

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

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

resumeDispatcher
Link copied to clipboard
runBlockingTest
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.

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

A version of runBlockingTest that works with TestScope.

runCurrent
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.

runTest
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.

runTestWithLegacyScope
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.

setMain
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.

StandardTestDispatcher
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.

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

A coroutine scope for launching test coroutines using TestCoroutineDispatcher.

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

Creates a TestScope.

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

Creates an instance of an unconfined TestDispatcher.

Properties

currentTime
Link copied to clipboard
val TestScope.currentTime: Long

The current virtual time on testScheduler.

currentTime
Link copied to clipboard

The current virtual time on testScheduler.

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

List of uncaught coroutine exceptions, for backward compatibility.