In Compose UI testing, which of a screen's tests can run on Robolectric rather than a device, and why should a captureToImage baseline come from the same environment?
answer
- test source set versus androidTest
- simulated Android in the JVM
- system UI and WebView need a device
- API 26 for captureToImage, 28 for dialogs
- rendering differs per environment
basics
~20 sBehaviour tests run well on Robolectric; system UI such as edge-to-edge or picture-in-picture, and unsupported features like WebView, need a device. captureToImage pixels depend on renderer and density, so compare them only with baselines from the same environment.
solid answer
~40 sThe same Compose test code runs in either place: in the `test` source set with Robolectric, which simulates Android inside the JVM, or in `androidTest` on a device or emulator. Android's guidance is to use Robolectric for isolated UI behaviour tests, such as checking that the rehearsal list changes after a filter click, and to keep device tests for system UI like edge-to-edge or picture-in-picture and for unsupported features like `WebView`. `captureToImage()` returns an `ImageBitmap` of the node's region; it needs API 26, and dialogs need API 28. It only captures: comparing with a recorded baseline is my own code or a screenshot-testing library. Robolectric renders differently from a GPU device, and density changes the bitmap's size, so a baseline is only comparable with captures from the same environment.
code
kotlin · 14 lines// build.gradle.kts of the app module
android {
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}
dependencies {
testImplementation("org.robolectric:robolectric:$robolectric_version")
testImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
}go deeper
Recall that Compose tests can run in the test source set on Robolectric or in androidTest on a device, and that captureToImage returns an ImageBitmap.
Explain how a test moves to Robolectric and what captureToImage needs: API 26, API 28 for dialogs, and an idle UI before capturing.
Split a screen's tests between Robolectric and devices by what each needs, and keep pixel baselines tied to one pinned environment.
Decide the team's balance between fast JVM UI tests and a device suite, and who owns pinned screenshot environments and baseline updates.
## Two places to run the same test A Compose UI test written with `createComposeRule()` and semantics finders does not care where it runs: - **Instrumented** (`src/androidTest`): the test APK runs on an emulator or device, with the real Android framework, GPU rendering and system UI. - **Robolectric** (`src/test`): Robolectric, an open-source framework maintained by Google, simulates the Android framework inside the JVM, supporting every version since API level 21. No emulator boots, so tests start fast and avoid emulator flakiness. Moving a test across is a build change, not a code change: 1. Move the test class to the `test` source set. 2. Add Robolectric as a `testImplementation` dependency, along with the Compose test artifacts. 3. Set `android.testOptions.unitTests.isIncludeAndroidResources = true` so resources are available. 4. Run it under `@RunWith(AndroidJUnit4::class)`; any UI test in the `test` source set runs with Robolectric. ## What fits where Android's Robolectric guidance is a per-test judgement call, summarised here: | Test | Robolectric | Device or emulator | |---|---|---| | Behaviour: click a voice-part filter, assert the list changed | yes, fidelity is more than enough | possible, slower | | Component state at another font size or theme | yes, when pixel accuracy is not critical | yes | | System UI: edge-to-edge insets, picture-in-picture | no | yes | | Features Robolectric does not support, such as `WebView` | no | yes | | Pixel-exact screenshots compared against a baseline | lower fidelity | yes, on a pinned device configuration | Robolectric has no screen like an emulator does and supports some APIs only partially, so a green Robolectric run does not prove the screen draws correctly on hardware. Many teams therefore run the bulk of Compose behaviour tests on Robolectric on every change and keep a smaller device suite for what needs the real platform. ## What captureToImage does `SemanticsNodeInteraction.captureToImage()` captures the found node's area of its window into an `ImageBitmap`: - It is annotated `@RequiresApi(Build.VERSION_CODES.O)`, API 26, so an instrumented test on a minSdk 24 app should be limited to API 26 and up. - A node inside a dialog can only be captured from API 28; below that the call throws `IllegalArgumentException`. - Since Compose 1.12, selecting several roots, for example `onRoot()` while a popup or dialog is open, captures the popup or dialog together with its anchor in one bitmap. - It captures what has been drawn. After driving the clock by hand, call `waitForIdle()` first, because on Android the draw pass is not driven by the test clock; keep animations settled or frozen at a known frame. `captureToImage()` does **not** compare anything. Comparing the bitmap with a recorded baseline, choosing a tolerance and storing the files is code the team writes, or a screenshot-testing library provides; tolerance and baseline-review practice are a visual-regression topic of their own. ## Why baselines are environment-specific - **Renderer:** Robolectric draws with its own simulation, a device with its GPU stack; the Android docs note that different devices render screens slightly differently and rate Robolectric's screenshot fidelity lower. - **Density and size:** the same composable produces a bitmap of a different pixel size on a different density, so the images cannot even be compared pixel for pixel. - **Fonts and platform version:** text rasterisation and system fonts can differ between API levels and devices. So record the baseline in the environment that will check it: the same Robolectric setup, or the same device or emulator image, API level and density. Comparing a device capture against a Robolectric baseline produces failures that mean nothing. ## Keeping baselines trustworthy Even inside one environment, a capture is only stable if everything that feeds the pixels is fixed: - Fake data with fixed dates and names, so "Tue 19:00" never becomes today's date. - A settled UI: capture after `waitForIdle()`, or with `mainClock.autoAdvance = false` at a known frame, never mid-animation by accident. - Fixed configuration: `DeviceConfigurationOverride` with `FontScale`, `DarkMode` or `ForcedSize` pins those values locally for the composable under test instead of depending on the device's settings. - One owner for re-recording baselines, so an intended redesign updates them deliberately and an accidental change fails. ## Checklist for a screen like the rehearsal schedule - Filter, load and confirmation behaviour: Robolectric, fake data, the v2 rule. - Edge-to-edge layout under the system bars: device. - A pixel baseline of the rehearsal card: one pinned environment, captured after idle, compared by the team's helper.
- A captureToImage test of a confirmation dialog fails on an API 27 emulator; why?Capturing a node inside a dialog needs API 28, and below that `captureToImage()` throws `IllegalArgumentException`. Run that test only on API 28 and up, or capture the content without the dialog window.
- Should a Robolectric run replace the device suite entirely?No. Robolectric gives enough fidelity for most behaviour tests, but Android's guidance keeps device tests for system UI such as edge-to-edge and picture-in-picture, for unsupported features like WebView, and for pixel-accurate screenshots. Keep a smaller device suite for those.
- Robolectric needs a different Compose test API from device tests
- A Robolectric pass proves edge-to-edge insets look right
- captureToImage compares the bitmap against a stored baseline
- A device baseline can check Robolectric captures with a small tolerance
- captureToImage works on every API level the app supports
Assumes Compose UI testing 1.12, whose captureToImage can capture a popup or dialog together with its anchor; the call itself requires API 26.