Flaky tests

Testing Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

An Espresso test sometimes fails because a list hasn't loaded yet. Someone added Thread.sleep(2000). Better fix?

Options
Hint 1 of 1

How can the test know the load finished?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Inject a test dispatcher or register an IdlingResource so Espresso waits for the load

Correct. The test waits exactly as long as the work takes.

Why not the others?
  • B Increase the sleep to 5000. Slower and still flaky on slow devices.
  • C Retry the test 3 times. Hides the problem.
  • D Run the test on a faster emulator. Timing still varies.

30-second answer

Flaky tests pass and fail without code changes. Common causes: real time (sleeps, timeouts), work the framework doesn't know to wait for (custom threads, uncontrolled dispatchers), animations, shared state between tests, real network, and ordering assumptions. Fix by injecting test dispatchers and fakes, using idling resources or Compose's automatic sync, disabling animations, and isolating state per test.

Practise saying it · 30 s. No recording, just a timer

Explain flaky tests and fixes.

Say your answer out loud, then check which of these points you covered:

  • Real time and unsynced work
  • Animations and shared state
  • Inject dispatchers and fakes
  • Idling resources
  • Isolate state

Deep dive

Run a suspicious test hundreds of times to reproduce.

Quarantine known-flaky tests and track them; don't just retry forever.

Test Orchestrator runs each test in its own Instrumentation; add clearPackageData to also wipe app data between tests.

Avoid depending on test execution order.

Common mistakes

  • Adding Thread.sleep.
  • Retrying failed tests automatically and calling it fixed.
  • Tests that depend on the device clock or locale.

Senior take & follow-ups

Name causes and fixes concretely; mention measuring flakiness.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does Test Orchestrator do?

    Show hint

    Runs each test in its own Instrumentation so in-memory state can't leak between tests; with clearPackageData it also clears app data.

  2. Why are sleeps bad?

    Show hint

    Too short is flaky, too long is slow; neither syncs with real work.

Similar on iOS: Testing concurrent code without sleeps The same timing problems appear in XCTest with async code.

Suggest a fix if something here is wrong or out of date.