android-testing
Android testing patterns for unit tests, instrumented tests, Compose UI tests, screenshot tests, and end-to-end journeys. Use when setting up JUnit4/JUnit5 + Robolectric, wiring Hilt/Koin for tests, choosing between Espresso and Compose Test APIs, adding Roborazzi/Paparazzi/Dropshots screenshot test
v2026.11.13-21cd5683 התקנות+3 ב-7 ימים39 כוכביםMITסונכרן לפני 11 שעות
התקנה
עדיין אין לכם את ה-CLI של NotHoly? התקינו אותו קודם.
$ curl -fsSL https://notholy.com/install.sh | sh$ notholy skills install aeondave/android-testingנכתב אל ~/.notholy/skills אחרי בדיקת החתימה וה-SHA-256.
חתימהחתום, ed25519
חבילה6da3856d…95a219db
6da3856df9d5…68db95a219db6 קבצים · 16.4 KBסריקהבינוני3 ממצאים
- injection.no_confirmationSKILL.md:27
- injection.no_confirmationreferences/instrumented-and-e2e.md:70
- injection.no_confirmationreferences/screenshot-testing.md:21
מקורgithub.com/AeonDave/malskill/tree/main/coding/android-testing@21cd568
הפריט הזה עדיין לא נכתב בעברית, ולכן הוא מוצג בשפה אחרת: English.
בעמוד הזה
Test infrastructure and strategy for native Android apps. Pair with kotlin-patterns for Kotlin/coroutine idioms and test-driven-development when implementing test-first.
When to activate
- Bootstrapping a testing setup on a new or legacy Android module
- Adding coverage for ViewModels, repositories, Compose screens, or navigation
- Choosing between unit, Robolectric-hosted, or on-device tests
- Introducing screenshot testing (local vs on-device)
- Diagnosing flakes in Espresso/Compose tests or emulator CI runs
Core rules (high signal)
- Match the existing test stack — do not silently swap JUnit4 for JUnit5, or Mockito for MockK, in a codebase committed to the other. Migrate deliberately.
- Push tests down. Prefer JVM unit tests (
src/test/) with fakes; use Robolectric only when the code under test genuinely needs Android framework classes; use instrumented tests (src/androidTest/) only for on-device concerns (Room migrations,WorkManager, biometric, camera, notifications, IME). - Coroutines: inject dispatchers; use
runTest { }(notrunBlocking) withMainDispatcherRule(orDispatchers.setMain(StandardTestDispatcher())in@Before/@After). - Fakes over mocks when the collaborator has behavior; mocks only for boundary interfaces you cannot re-implement cheaply. Mocking a DAO with 30 methods is a smell.
- Compose UI tests run under Robolectric (
RobolectricTestRunner+AndroidJUnit4) for speed; run on-device only when validating system UI (edge-to-edge, notifications, IME insets, IPC dialogs). - Never
Thread.sleepin a test. UseIdlingResource,composeTestRule.waitUntil { },advanceTimeBy(...), orTurbine.awaitItem().
Setup baseline
If no framework is chosen, install:
- JUnit4 (
junit:junit) — still the standard for AndroidX Test/Espresso/Compose; JUnit5 requires thede.mannodermaus.gradle.plugins.android-junit5plugin and does not work for instrumented tests. - AndroidX Test (
androidx.test:core,androidx.test:runner,androidx.test:rules,androidx.test.ext:junit). - Robolectric (
org.robolectric:robolectric) for JVM-hosted Android tests. - Compose Test (
androidx.compose.ui:ui-test-junit4,androidx.compose.ui:ui-test-manifestindebugImplementation). - Coroutines test (
org.jetbrains.kotlinx:kotlinx-coroutines-test). - Turbine (
app.cash.turbine:turbine) forFlowassertions. - MockK (
io.mockk:mockk) only if mocking is actually needed; do not preinstall. - Jacoco for coverage (
jacocoplugin per module).
Optional based on need:
- Espresso (
androidx.test.espresso:espresso-core,-contrib,-intents) — for XML/View tests. - UI Automator (
androidx.test.uiautomator:uiautomator) — for cross-app end-to-end. - Screenshot testing: pick one primary local framework:
- Roborazzi (
io.github.takahirom.roborazzi:roborazzi) — Robolectric-based, cross-platform PNG diff, fast. - Paparazzi (
app.cash.paparazzi:paparazzi) — LayoutLib-based, no Robolectric, no emulator. Versions ≤ 2.0.0-alpha04 have known issues with AGP 9. - Compose Preview Screenshot Testing — AGP-version split: AGP 9.5.0-alpha03+ configures it natively via
testOptions { screenshotTests { create(...) } }(test suites); older AGP needs the legacy standalone plugincom.android.compose.screenshot, which is deprecated. Never mix both setups in one module. - Dropshots (
com.dropbox.dropshots:dropshots) — on-device screenshots for scenarios that require real system UI.
- Roborazzi (
Add the AndroidX test instrumentation runner to the module build.gradle.kts:
android {
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
// With Hilt: "com.example.HiltTestRunner"
}
testOptions {
unitTests.isIncludeAndroidResources = true // required for Robolectric to read res/
unitTests.isReturnDefaultValues = true // avoid throwing on unmocked Android calls in pure JVM tests
}
}AGP 9 notes (breaking, Jan 2026)
AGP 9.0 changes defaults and DSL in ways that touch test wiring:
- Built-in Kotlin:
org.jetbrains.kotlin.androidis no longer applied/compatible; AGP ships KGP 2.2.10 runtime. Minimums: Gradle 9.1, JDK 17, KGP 2.2.10 (Hilt 2.59.2+, KSP 2.3.6+ if used). - New-DSL only: legacy
androidblock APIs removed (android.newDsl=truedefault; opt-out removed in AGP 10). UseandroidComponentsinstead ofapplicationVariants/variantFilter. - Unit-test defaults:
android.onlyEnableUnitTestForTheTestedBuildType=true— unit test tasks exist only fordebugunless re-enabled; CI invokingtestReleaseUnitTestbreaks. Tooling renames "Android Unit Test" → "Android Host Test"; re-check task names when wiring CI. - Runner default:
android.default.androidx.test.runner=true—AndroidJUnitRunneris the default; keep the explicittestInstrumentationRunneronly for custom runners (e.g. Hilt). android.useAndroidx=trueandandroid.uniquePackageNames=trueenforced by default.
Test placement
| Sourceset | Runs on | Use for |
|---|---|---|
src/test/ |
JVM (host) | Pure Kotlin logic, ViewModels with fake repositories, coroutines with runTest, Robolectric-hosted UI tests, local screenshot tests |
src/androidTest/ |
Device / emulator | Room DB (real SQLite), WorkManager, biometric, camera, notifications, IME insets, edge-to-edge, cross-app IPC (UI Automator) |
src/testFixtures/ |
Both | Shared test doubles (fakes, factories) usable from test and androidTest |
- Do not place Espresso or Compose UI tests in
androidTest/by default — run them under Robolectric intest/for speed unless a real device concern is being validated.
Coroutine test pattern
class MainDispatcherRule(
private val dispatcher: TestDispatcher = StandardTestDispatcher(),
) : TestWatcher() {
override fun starting(d: Description) = Dispatchers.setMain(dispatcher)
override fun finished(d: Description) = Dispatchers.resetMain()
}
class FeedViewModelTest {
@get:Rule val main = MainDispatcherRule()
@Test fun loads() = runTest {
val vm = FeedViewModel(FakeRepo(items = listOf(Item("a"))))
vm.refresh()
advanceUntilIdle()
assertEquals(UiState.Loaded(listOf(Item("a"))), vm.state.value)
}
}Rules:
- Inject dispatchers (never call
Dispatchers.IOdirectly in production code you want to test). - Use
advanceUntilIdle(),advanceTimeBy(ms), orrunCurrent()— neverThread.sleep. - Turbine for
Flowassertions:kotlin vm.state.test { assertEquals(UiState.Loading, awaitItem()) vm.refresh() assertEquals(UiState.Loaded(...), awaitItem()) }
Compose UI tests
@RunWith(AndroidJUnit4::class) // Robolectric-hosted when in src/test/
class FeedScreenTest {
@get:Rule val compose = createAndroidComposeRule<ComponentActivity>()
@Test fun rendersItems() {
compose.setContent { MyTheme { FeedScreen(state = loaded(listOf("a"))) } }
compose.onNodeWithText("a").assertIsDisplayed()
compose.onNodeWithTag("refresh").performClick()
compose.waitUntil { compose.onAllNodesWithTag("row").fetchSemanticsNodes().size >= 2 }
}
}Matcher order: semantic first (onNodeWithText, onNodeWithContentDescription, hasSetTextAction()). Fall back to testTag("…") only when three or more semantic matchers would be needed.
State restoration is a common regression source. Wrap the composable in a StateRestorationTester(compose) and call emulateSavedInstanceStateRestore() in tests for screens with saved state.
Simulate window sizes and font scale without devices via DeviceConfigurationOverride (Compose 1.8+): wrap the content under test with the overridden WindowWindowSizeClass/font-scale values instead of firing up tablet emulators.
Espresso (View-based)
- Base matchers:
onView(withId(R.id.…)),onView(withText("…")). - Actions:
perform(click(), typeText(), scrollTo()). - Assertions:
check(matches(isDisplayed())). - Idling: register a custom
IdlingResourcefor background work; do notThread.sleep.
Only load Espresso when the module still has XML views; do not use it in Compose-only modules.
Screenshot tests
Local (fast) shape:
- Screen-level: 9-cell grid — widths
{400, 610, 900}dp × heights{400, 500, 1000}dp. One PNG per cell per screen. Add extra shots for alt themes and font scale1.5. - Component-level: theme × font-scale matrix per public composable.
Roborazzi example (JVM/Robolectric):
@RunWith(AndroidJUnit4::class)
class ProfileScreenshotTest {
@get:Rule val compose = createAndroidComposeRule<ComponentActivity>()
@Test fun light() {
compose.setContent { MyTheme(dark = false) { ProfileScreen(sampleUser) } }
compose.onRoot().captureRoboImage("build/reports/screenshots/profile_light.png")
}
}- Reference PNGs go under
src/test/screenshots/(Roborazzi) orsrc/test/snapshots/(Paparazzi). Commit them; a repo without stored baselines cannot regress. - On-device screenshot tests (Dropshots) only for real-system scenarios (edge-to-edge with system bars, IME rendering, notification shade, PiP).
Room and databases
- Instrumented tests: use in-memory Room to avoid file pollution:
kotlin Room.inMemoryDatabaseBuilder(context, AppDb::class.java) .allowMainThreadQueries() // tests only .build() - Migrations:
MigrationTestHelperreproduces prior schemas; verify each migration and add a bulk test that runs all migrations end-to-end.
Dependency injection for tests
- Hilt:
@HiltAndroidTeston the class,@get:Rule val hilt = HiltAndroidRule(this). Use a customHiltTestRunnerextendingAndroidJUnitRunnerthat returnsHiltTestApplication. - Replace bindings per test with
@BindValue(single-test) or@Module @TestInstallIn(...)(per module). - Koin:
KoinTestRulewith an overridemodule { single<Repo> { FakeRepo() } }.
Do not add production seams solely for tests; use DI to inject fakes.
Navigation tests
- Compose Navigation: use
TestNavHostController(Compose) — set the graph, drive the ViewModel, assert onnavController.currentBackStackEntry?.destination?.route. - Test back handling, deep links, and multi-back-stack "exit through home" flows separately.
End-to-end (release-candidate) tests
- Keep the count small (roughly 5% of total tests). One journey per critical flow.
- Use Compose Test / Espresso for in-app; hand off to UI Automator for cross-app (notification panel, share sheet, system settings).
- Prefer running on a real device profile in CI (Firebase Test Lab, Gradle-managed devices) over headless emulator for these.
Quick review checklist
- No
runBlocking; every coroutine test usesrunTestwith aMainDispatcherRule - No
Thread.sleep; every wait usesadvanceTimeBy,waitUntil,IdlingResource, or Turbine - Espresso tests do not exist in Compose-only modules
- Compose UI tests use semantic matchers first,
testTagonly when needed - Screenshot references are committed; baselines exist for every added test
- Room instrumented tests use in-memory DB; migrations have explicit tests
- Hilt tests use
HiltTestApplicationvia a custom runner;@BindValueswaps are scoped - CI runs
-race-equivalent invariants:./gradlew testandconnectedDebugAndroidTest(or Gradle-managed device tasks) both green
Common flake sources
- Real
Dispatchers.IO/Dispatchers.Defaultused inside code under test → intermittent ordering. Fix: inject dispatchers. - Global mutable state (companion object caches,
objectsingletons) across tests → order-dependent failures. Fix: reset in@Afteror scope to DI. SharedFlow/Channelconsumed viafirst()when no emission is guaranteed → hang. Fix: Turbine with a timeout.- Espresso
IdlingResourcenot unregistered → next test hangs waiting on stale registry. Fix: register/unregister via@Rule. - Screenshot diffs on different host OS (font rendering differs between Linux CI and macOS/Windows dev). Fix: standardize on Linux for baselines; use Docker if needed.
- Robolectric SDK level mismatch between test and app → resource lookups fail. Set
@Config(sdk = [35])or configure globally inrobolectric.properties. SDK 36 testing requires Robolectric 4.16+ and JDK 21; Robolectric 4.15 tops out at SDK 35.
Resources
Load on demand:
- references/coroutine-and-flow-testing.md —
runTest, virtual time, dispatcher injection, Turbine patterns, cancellation testing; load when writing suspend/Flow tests - references/compose-ui-testing.md — semantic matchers,
waitUntil,StateRestorationTester, gesture testing, hilt-in-compose tests; load when writing Compose tests - references/screenshot-testing.md — framework comparison (Roborazzi, Paparazzi, Compose Preview Screenshot, Dropshots), matrix design, baseline hygiene; load when adding or fixing screenshot coverage
- references/hilt-and-di-testing.md —
HiltTestApplication, custom runner,@BindValue/@TestInstallIn, KoinKoinTestRule; load when setting up DI-aware tests - references/instrumented-and-e2e.md — androidTest sourceset, in-memory Room,
MigrationTestHelper,WorkManagertesting, UI Automator for cross-app flows; load when a test must run on a device or emulator
הקבצים בגרסה הזו
references/531.4 KB
- compose-ui-testing.md6.4 KB
- coroutine-and-flow-testing.md5.3 KB
- hilt-and-di-testing.md5.9 KB
- instrumented-and-e2e.md7.6 KB
- screenshot-testing.md6.2 KB
- SKILL.md14.2 KB
הרשאות
| הרשאה | הוצהר | נמצא בסריקה |
|---|---|---|
| קורא קבצים | לא | לא |
| כותב קבצים | כן | כן |
| מריץ פקודות shell | לא | לא |
| משתמש ברשת | לא | לא |
| משתמש במחברים | לא | לא |
| שולט בדפדפן | לא | לא |
ממצאי הסריקה
נבדק ב-13 בנוב׳ 2026
- בינוניתPrompt injection · injection.no_confirmationSKILL.md:27
- בינוניתPrompt injection · injection.no_confirmationreferences/instrumented-and-e2e.md:70
- בינוניתPrompt injection · injection.no_confirmationreferences/screenshot-testing.md:21
סריקה נקייה מורידה את הסיכון, אבל לא מוכיחה שהחבילה בטוחה. כדאי לקרוא מה מתקינים.
| גרסה | פורסמה | Commit | SHA-256 | חתימה | שינוי בהרשאות |
|---|---|---|---|---|---|
| v2026.11.13-21cd568 | 13 בנוב׳ 2026 | 21cd568 | 6da3856d | חתומה | – |
עוד מ-AeonDave
הכול מהמפרסם הזהעוד בקטגוריה מובייל, קושחה וחומרה
לקטגוריהשאלו כל מה שהסקופ מאפשר.
יוצרים חשבון, מוסיפים קרדיטים ושואלים את השאלה הראשונה בצ׳אט או בטרמינל.
- 1/3
יצירת חשבון
פרטי או חברה. אפשר לעבור לחשבון חברה אחר כך בלי לאבד יתרה או מפתחות.
פרטיחברה - 2/3
הוספת קרדיטים
בכרטיס אשראי. תשלום לפי טוקן, בלי מנוי.
$5.00טעינה מינימלית - 3/3
שאלה ראשונה
בצ׳אט, או להתקין את ה-CLI ולהריץ notholy login בטרמינל.
$ notholy login