Compose & Espresso UI Testing Interview Questions

TESTING › UI

How do you write a basic Compose UI test?

What a strong answer covers: Use createComposeRule() (or createAndroidComposeRule for an Activity), call setContent with the composable, then find nodes with onNodeWithText, onNodeWithTag or onNodeWithContentDescription, perform actions like performClick or performTextInput, and assert with assertIsDisplayed, assertTextEquals or assertExists. Compose tests read the semantics tree, not the rendered pixels.

What is the semantics tree, and when should you use test tags?

What a strong answer covers: Semantics is the description of the UI used by accessibility services and tests: text, content descriptions, roles, states and actions. Tests should find nodes the way a user would, by text or content description, which also checks accessibility. Use Modifier.testTag when there's no meaningful user-visible label or text is ambiguous; useUnmergedTree when you need children of merged nodes.

Espresso vs Compose testing vs UI Automator: what is each for?

What a strong answer covers: Espresso tests View-based UI inside your app, synchronising automatically with the main thread and idling resources. Compose testing APIs test composables, synchronising with recomposition and animations. UI Automator interacts across apps and with the system UI (notifications, permission dialogs, settings), useful for end-to-end and Macrobenchmark scenarios.

Why are UI tests flaky, and how do you make them reliable?

What a strong answer covers: Causes: async work the framework doesn't know about (custom threads, real network), animations, timing assumptions, shared state between tests, device differences and real backends. Fixes: inject test dispatchers and fake repositories, register idling resources or use waitUntil with conditions, disable animations, use hermetic test data, run on consistent emulators (Gradle managed devices), use the test orchestrator to isolate tests, and avoid sleep.

What is screenshot testing, and how would you set it up for a design system or Compose screens?

What a strong answer covers: Screenshot tests render a composable and compare it to a stored golden image, catching visual regressions that assertions miss. Tools: Paparazzi (JVM, no device), Roborazzi (with Robolectric), and Compose Preview Screenshot Testing. Render previews for different themes, font scales, locales and sizes; record goldens and verify in CI; keep tolerance low; and review image diffs in pull requests. Deterministic rendering (fixed fonts, no animations, fixed data) matters.

Back to Compose & Espresso UI Testing