agentleFS
wrinfotelwrinfotel/verified-android-skills/skills/android-testing/SKILL.md

android-testing

Skill0 starsChanged 32 days agoMITView on GitHub

Android testing discipline: test taxonomy and source sets, Gradle scopes for test-only dependencies, Robolectric placement, the deprecated ActivityTestRule, and Espresso synchronization without sleeps. Use when writing or reviewing local unit tests, instrumented tests, Robolectric tests, or Espresso tests. Core rules have machine checks in checks/.

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

10 rules. 0 are common, 3 are distinctive, and 7 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    After ANY Android test edit, run the Verification step (bottom of this file).

    line 16
  2. Very specific:

    MUST put JVM tests in module-name/src/test/ and device tests in module-name/src/androidTest/.

    line 21
  3. Very specific:

    MUST live in src/test/, never in src/androidTest/.

    line 24
  4. Very specific:

    MUST declare test-only libraries in the scope of the source set that uses them: testImplementation for local tests (JUnit, Mockito, MockK, …

    line 45
  5. Very specific:

    MUST NOT use androidx.test.rule.ActivityTestRule (deprecated).

    line 72
  6. Very specific:

    MUST NOT Thread.sleep / SystemClock.sleep in instrumented tests.

    line 91
  7. Distinctive:

    Test doubles: prefer fakes over mocks — the official fundamentals examples construct the object under test with a fake repository, and the …

    line 114
  8. Distinctive:

    Test pyramid balance, CI sharding, parameterized tests — strategy decisions, not text patterns.

    line 118
  9. Distinctive:

    Run after every Android test edit (from the project root):

    line 123
  10. Very specific:

    All four must pass (exit 0) before declaring the task done.

    line 132

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Android Testing

Operational rules for a coding agent. RIGHT/WRONG pairs are canonical. After ANY Android test edit, run the Verification step (bottom of this file). Compose UI tests are covered by the compose-testing skill.

1. Taxonomy — where a test lives

MUST put JVM tests in module-name/src/test/ and device tests in module-name/src/androidTest/. Robolectric runs a simulated Android environment inside a JVM — it is a local-test tool: Robolectric tests MUST live in src/test/, never in src/androidTest/. The official conversion path says exactly that: an instrumented test becomes a Robolectric test by moving it to the test source set.

Robolectric setup (both pieces required):

android {
    testOptions { unitTests { isIncludeAndroidResources = true } }
}
dependencies {
    testImplementation("org.robolectric:robolectric:4.13")
}

WHY: androidTest code needs a device or emulator and runs an order of magnitude slower. A Robolectric test parked in androidTest silently turns a fast local suite into an instrumentation run.

2. Dependency scope — test libs never ship in the APK

MUST declare test-only libraries in the scope of the source set that uses them: testImplementation for local tests (JUnit, Mockito, MockK, Robolectric), androidTestImplementation for instrumented tests (AndroidX Test core/runner/rules, Espresso, UI Automator). MUST NOT put them on implementation/api — that puts the test framework on the production classpath and ships it in the APK.

WRONG:

implementation("junit:junit:4.13.2")
implementation("io.mockk:mockk:1.13.11")

RIGHT:

testImplementation("junit:junit:4.13.2")
testImplementation("io.mockk:mockk:1.13.11")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")

Documented exception: espresso-idling-resource may be implementation when production code references it — the set-up-project guide allows both, depending on whether the dependency belongs on the app or the test APK classpath.

3. Activity rule — no deprecated ActivityTestRule

MUST NOT use androidx.test.rule.ActivityTestRule (deprecated). Use ActivityScenarioRule (androidx.test.ext.junit.rules) or ActivityScenario (androidx.test.core.app); to touch the Activity under test use scenario.onActivity {}.

WRONG:

@get:Rule
val rule = ActivityTestRule(LoginActivity::class.java)

RIGHT:

@get:Rule
val rule = activityScenarioRule<LoginActivity>()

4. Synchronization — Espresso tracks idle state

MUST NOT Thread.sleep / SystemClock.sleep in instrumented tests. Espresso keeps track of the app under test's idle state (message queue, AsyncTasks, registered idling resources) and lets you stay away from waits, syncs, sleeps and polls. For background work Espresso cannot see (custom threads, network callbacks), register an IdlingResource — do not sleep.

WRONG:

onView(withId(R.id.greet_button)).perform(click())
Thread.sleep(2_000)
onView(withText("Hello Steve!")).check(matches(isDisplayed()))

RIGHT:

onView(withId(R.id.greet_button)).perform(click())
onView(withText("Hello Steve!")).check(matches(isDisplayed()))
// synchronization is built in

5. What the checks cannot see (review-verified)

  • Test doubles: prefer fakes over mocks — the official fundamentals examples construct the object under test with a fake repository, and the local-tests guide says to avoid complex mocking and use fakes (or Robolectric shadows) instead.
  • Test pyramid balance, CI sharding, parameterized tests — strategy decisions, not text patterns.

Verification

Run after every Android test edit (from the project root):

bash <skill-dir>/checks/check_robolectric_source_set.sh   <project-or-src-dir>
bash <skill-dir>/checks/check_test_dep_scope.sh           <project-or-src-dir>
bash <skill-dir>/checks/check_no_activity_test_rule.sh    <test-src-dir>
bash <skill-dir>/checks/check_no_sleep_in_instrumented.sh <test-src-dir>

All four must pass (exit 0) before declaring the task done. Checks are deterministic heuristics — they catch the documented anti-patterns above, not every testing bug. §5 is verified by review against references/testing-android.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.