Skip to main content
Version: Next

Espresso

Tools to provide Espresso functionality for coroutines.

If an IdlingDispatcherProvider is registered with the IdlingRegistry, Espresso will wait for all associated coroutines to leave the active state before performing any assertions.

Coroutines which are in a suspended state (such as a Job "observing" a Flow for updates) do not prevent Espresso from performing assertions.

class IdlingCoroutineScopeRuleWithLifecycleSample {

// Retrieve the DispatcherProvider from a dependency graph,
// so that the same one is used throughout the codebase.
val customDispatcherProvider = testAppComponent.customDispatcherProvider

@JvmField
@Rule
val idlingRule = IdlingDispatcherProviderRule {
IdlingDispatcherProvider(customDispatcherProvider)
}

/**
* If you don't provide CoroutineScopes to your lifecycle components via a dependency injection framework,
* you need to use the `dispatch-android-lifecycle-extensions` and `dispatch-android-viewmodel` artifacts
* to ensure that the same `IdlingDispatcherProvider` is used.
*/
@Before
fun setUp() {
LifecycleScopeFactory.set {
MainImmediateCoroutineScope(customDispatcherProvider)
}
ViewModelScopeFactory.set {
MainImmediateCoroutineScope(customDispatcherProvider)
}
}

@Test
fun testThings() = runBlocking {

// Now any CoroutineScope which uses the DispatcherProvider
// in TestAppComponent will sync its "idle" state with Espresso

}

}

Types

NameDescription
IdlingDispatcherProviderRuleJUnit 4 Rule which automatically registers an IdlingDispatcherProvider with the IdlingRegistry
IdlingDispatcherA CoroutineDispatcher which tracks each dispatched coroutine using a CountingIdlingResource. All actual dispatches are delegated to a provided CoroutineDispatcher.
IdlingDispatcherProviderA special DispatcherProvider which guarantees that each of its properties is an IdlingDispatcher
IdlingCoroutineScopeA special CoroutineScope which guarantees a property of an IdlingDispatcherProvider

IdlingCoroutineScopes

Marker InterfaceFactory FunctionDescription
DefaultIdlingCoroutineScopeDefaultIdlingCoroutineScopeA IdlingCoroutineScope with a CoroutineDispatcher of default.
IOIdlingCoroutineScopeIOIdlingCoroutineScopeA IdlingCoroutineScope with a CoroutineDispatcher of io.
MainIdlingCoroutineScopeMainIdlingCoroutineScopeA IdlingCoroutineScope with a CoroutineDispatcher of main.
MainImmediateIdlingCoroutineScopeMainImmediateIdlingCoroutineScopeA IdlingCoroutineScope with a CoroutineDispatcher of mainImmediate.
UnconfinedIdlingCoroutineScopeUnconfinedIdlingCoroutineScopeA IdlingCoroutineScope with a CoroutineDispatcher of unconfined.

Minimum Gradle Config

Add to your module's build.gradle.kts:

repositories {
mavenCentral()
}

dependencies {

// core
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2")
implementation("com.rickbusarow.dispatch:dispatch-core:1.0.0-beta10-SNAPSHOT")

androidTestImplementation("com.rickbusarow.dispatch:dispatch-android-espresso:1.0.0-beta10-SNAPSHOT")

// android
androidTestImplementation("androidx.test:runner:1.3.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}