skip to content

As Android lead, how large a share of UI tests should be Espresso tests, and do you standardise on idling resources or injected idling-aware dispatchers for synchronisation?

level: principalimportance: should knowfreq 22%

answer

  1. device time is the scarce resource
  2. what only a real View tree proves
  3. counters in app code versus executors
  4. suspended work is not a running task
  5. one rule the whole suite follows

basics

~20 s

Keep Espresso for flows that need a real View hierarchy on a device and pin everything else lower. For synchronisation, pick one team standard: injected idling-aware executors cover most background work automatically, CountingIdlingResource covers the rest explicitly, and sleeps are banned.

solid answer

~50 s

There is no right percentage; the constraint is device time and flake rate. Espresso tests need an emulator or device, run slower and can flake, so I keep them for what only an instrumented View test proves: screen flows, real layouts and input, navigation, and intent handoffs stubbed with Espresso-Intents. Logic goes to local tests. For synchronisation I would standardise on one approach and put it in a shared test module. Injecting idling-aware executors, for example an `IdlingThreadPoolExecutor` turned into a coroutine dispatcher with `asCoroutineDispatcher()`, makes every task on them visible with no per-call code. It misses suspended work and other libraries' threads, so a `CountingIdlingResource` or a fake server covers those explicitly. Hand-placed counters alone are precise but easy to forget on a new path. Whichever wins, `Thread.sleep` is banned, animations are off in the CI image and timeouts are investigated, not raised.

go deeper

for a junior

Recall that Espresso tests run on a device and cost more than local tests, and that waiting for background work uses idling resources, never sleeps.

for a middle

Explain the difference between a CountingIdlingResource placed around calls and an idling-aware executor injected as a dispatcher, and what each one makes visible to Espresso.

for a senior

Identify the gap in executor-based idling for suspended coroutines and other libraries' threads, and show how a counter at the network boundary or a fake closes it.

for a principal

Set the criterion for which flows earn an Espresso test, a flake budget with quarantine, and one synchronisation standard built into shared rules so new screens are correct by default.

## The decision behind the question A lead is asked two linked things. First, how much of the UI-level testing should be **instrumented Espresso tests**, given that they are the most expensive tests the team runs. Second, how those tests should **synchronise with background work**, because that choice decides how often the suite flakes and how much test-only code ends up in the app. Neither has a single right answer; both have costs a lead should be able to state. ## How many Espresso tests Espresso tests run on a device or emulator in the app's process. Compared with local tests they: - need emulator capacity on CI and minutes per run that grow with the suite; - depend on the environment: animations, device speed, system dialogs; - fail for reasons that are not bugs when synchronisation is wrong. What they buy is what nothing cheaper proves: the real View hierarchy is inflated, real input reaches it, `RecyclerView` and `AdapterView` lists scroll, the activity is launched and recreated through `ActivityScenario`, and outgoing intents are checked with Espresso-Intents. A workable rule is to keep each flow that a user would notice breaking, such as signing in or completing a purchase, as an Espresso test, and to move validation logic, formatting and state handling into local tests that run in seconds. The share then follows from the app: a forms-heavy app has more screens worth one each than a single-screen tool. What a lead fixes is the **criterion**, not the percentage, plus a flake budget: a test that fails without a code change is quarantined and fixed, not retried until green. ## Three ways to synchronise | Approach | How Espresso learns of the work | Strength | Cost | |---|---|---|---| | `CountingIdlingResource` calls in app code | `increment()` and `decrement()` around each async call | Precise; covers any thread | Test-only API in production code; every new path must remember it, and an error path that skips `decrement()` causes a timeout | | Idling-aware executors injected in the test build | `IdlingThreadPoolExecutor` tracks running tasks; turned into a dispatcher with `asCoroutineDispatcher()` and supplied by dependency injection | Covers everything dispatched on it with no per-call code | Misses suspended coroutines and other libraries' own threads | | Hermetic fakes | A fake repository or local fake server answers quickly and deterministically | Removes network variance entirely | Still asynchronous unless the fake is synchronous, and it tests less of the real threading | The executor approach has a limit that has to be understood before it is standardised. `IdlingThreadPoolExecutor` counts **running tasks**. A coroutine that has suspended, in a `delay`, or waiting for a callback from an HTTP client's own threads, is not a task on that executor while it waits. Espresso therefore sees the app as idle at exactly the moment the network call is in flight. That gap is why a pure-dispatcher standard still needs an explicit counter at the network boundary, or a fake that completes before control returns. ## A standard a team can actually hold One workable standard combines the approaches by layer: 1. **Dispatchers are injected everywhere**, never hard-coded, so the test build can supply idling-aware ones from a shared test module. 2. **The network boundary carries one `CountingIdlingResource`**, incremented and decremented in a `finally` in a single wrapper, so individual features do not have to remember it. 3. **Instrumented tests run against fakes or a local fake server**, so a slow backend cannot flake them. 4. **Registration lives in one base rule** that registers in `@Before` and unregisters in `@After` with `IdlingRegistry`, so no test leaks a busy resource into the next. 5. **The environment is fixed**: the three animation scales are off in the CI emulator image. And a short list of bans: no `Thread.sleep`, no retry loops around assertions, and no raising `IdlingPolicies` timeouts to make a failure go away. ## What to say in the interview The answer that lands names the tradeoff rather than a tool: explicit counters are precise but rely on discipline; injected executors are automatic but blind to suspended work; fakes remove variance but test less. A lead picks the mix that makes the **default path correct**, so a developer adding a screen gets synchronisation without thinking about it, and records the rule where reviewers can enforce it.

  • Why can an Espresso suite that injects an IdlingThreadPoolExecutor-based dispatcher still flake on a network call?
    The executor counts running tasks. While a coroutine is suspended waiting for the HTTP client, which runs on its own threads, no task is running on that executor, so Espresso sees the app as idle and may assert before the response. Cover the network boundary with a `CountingIdlingResource` or run against a fake that completes without leaving the test's control.
  • How do you keep idling code from leaking into release builds if the team objects to it?
    Android's guidance lists the options: keep idling resources in a debug-only build variant, inject them through dependency injection in the test build, or implement them in test code against a small seam the app exposes. Each removes the test-only API from release, at the cost of a seam that must stay in sync with the production code it wraps.

  • Every UI behaviour should be tested through Espresso on a device
  • Thread.sleep is acceptable in instrumented tests if the value is generous
  • An idling-aware executor also covers coroutines suspended on network calls
  • Raising the idling timeout is a fine fix for a flaky suite
  • Idling resources must ship in release builds; there is no alternative