Loading...
Loading...
Use this skill to organize Android test source sets — `src/test/`, `src/androidTest/`, the community `src/sharedTest/` convention, and the modern KMP-style `androidHostTest` / `androidDeviceTest` split that Compose itself adopted after AGP 7.2 broke the classic `sourceSets` wiring. Includes the `testImplementation` / `androidTestImplementation` / `debugImplementation` configuration matrix per Google's `/training/testing/local-tests` and `/training/testing/instrumented-tests` pages. Use when the user asks "where do I put my tests", "test or androidTest", "sharedTest setup", "AGP 7.2 broke my sourceSets", "androidHostTest vs androidDeviceTest", "testImplementation vs androidTestImplementation vs debugImplementation", or "why is my Robolectric test in androidTest".
npx skill4agent add skydoves/android-testing-skills organizing-test-source-setssrc/test/src/androidTest/src/test/src/androidTest/src/sharedTest/androidHostTestandroidDeviceTestUnresolved reference: createComposeRulesrc/sharedTest/sourceSets { test.java.srcDir(...) }androidHostTestandroidDeviceTestsrc/androidTest/src/test/ui-test-junit4ui-test-manifest../../../compose/setup/configuring-test-dependencies/SKILL.md../../concepts/understanding-the-testing-pyramid/SKILL.mdAndroidJUnitRunner../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.mdcom.android.applicationcom.android.librarytestImplementationandroidTestImplementationdebugImplementation../../concepts/understanding-the-testing-pyramid/SKILL.mdsrc/test//training/testing/local-tests"By default, the source files for local unit tests are placed in. This directory already exists when you create a new project using Android Studio." —module-name/src/test/developer.android.com/training/testing/local-tests
"A local test runs directly on your own workstation, rather than an Android device or emulator. As such, it uses your local Java Virtual Machine (JVM)." —developer.android.com/training/testing/local-tests
testImplementation./gradlew test./gradlew testDebugUnitTestsrc/androidTest//training/testing/instrumented-tests"In your Android Studio project, you store the source files for instrumented tests in. This directory already exists when you create a new project and contains an example instrumented test." —module-name/src/androidTest/java/developer.android.com/training/testing/instrumented-tests
androidTestImplementation./gradlew connectedAndroidTest./gradlew connectedDebugAndroidTestAndroidJUnitRunnertestImplementationandroidTestImplementation"Note:adds dependencies for local tests andtestImplementationadds dependencies for Instrumented tests." —androidTestImplementationdeveloper.android.com/training/testing/local-tests
Configuration Source set Variant Ships in
-----------------------------------------------------------------------------------
implementation main all release + debug + tests
api main all release + debug + tests
debugImplementation debug only debug debug APK + androidTest APK
releaseImplementation release only release release APK only
testImplementation src/test/ all (host) JVM test classpath
testDebugImplementation src/test/ debug (host) JVM test classpath, debug only
androidTestImplementation src/androidTest/ all (device) androidTest APK
androidTestUtil n/a all orchestrator/util install (NOT classpath)testImplementationandroidTestImplementationui-test-junit4debugImplementationdebugandroidx.compose.ui:ui-test-manifestandroidx.fragment:fragment-testing-manifestandroidx.compose.ui:ui-test-manifestdebugImplementationandroidTestImplementation../../../compose/setup/configuring-test-dependencies/SKILL.mdTestManifestGradleConfigurationsrc/sharedTest/tasks/research/R8-android-fundamentals.mdis not mentioned by name on any of the six in-scopesrc/sharedTest/pages or on the fetched Hilt-testing / rules pages.developer.android.com/training/testing/...
sourceSetstestandroidTest// pre-AGP 7.2 — the classic sharedTest wiring
android {
sourceSets {
getByName("test").java.srcDir("src/sharedTest/java")
getByName("androidTest").java.srcDir("src/sharedTest/java")
}
}androidx.test.ext.junit.runners.AndroidJUnit4srcDir(...)LinkageError:core-testingtestImplementationandroidTestImplementationandroidHostTestandroidDeviceTestandroidHostTestandroidDeviceTestcommonTest
└── androidCommonTest (helpers shared by host + device)
├── androidDeviceTest (instrumentation APK on emulator/device)
└── androidHostTest (Robolectric on JVM)compose/ui/ui-test/src/androidHostTest/kotlin/androidx/compose/ui/test/RobolectricComposeTest.ktcompose/material3/material3/src/androidDeviceTest/.../SwitchTest.ktcommonTest// :feature:home/build.gradle.kts — production module
plugins { id("com.android.library") }
dependencies {
testImplementation(project(":feature:home-testing"))
androidTestImplementation(project(":feature:home-testing"))
}
// :feature:home-testing/build.gradle.kts — pure Kotlin or Android-library "test fixtures"
plugins { id("java-library") } // or com.android.library if Android types needed:feature:home-testingFakeUserRepositoryMainDispatcherRuletestFixturesandroid { testFixtures { enable = true } }testFixtures()testFixturesImplementationtestImplementationandroidTestImplementationdeveloper.android.com/training/testing/...sharedTesttasks/research/R8-android-fundamentals.mdWhere does this test go?
│
├── Pure Kotlin/JVM logic, no Android framework → src/test/
│ (testImplementation deps)
│
├── Robolectric (Android framework simulated on JVM) → src/test/
│ (testImplementation deps + Robolectric)
│ NOT src/androidTest/.
│
├── ViewModel + coroutines + fakes → src/test/
│
├── Real Room database integration → src/androidTest/
│ (small instrumented test per /fundamentals)
│
├── Espresso UI test → src/androidTest/
├── UiAutomator cross-app test → src/androidTest/
├── Compose UI test against real Activity → src/androidTest/
│ + debugImplementation("...:ui-test-manifest")
│
├── Compose UI test on Robolectric → src/test/
│ (testImplementation("...:ui-test-junit4") + Robolectric)
│
├── Test helper (FakeRepository, MainDispatcherRule) → :core-testing module OR src/sharedTest/
│ (with the AGP 7.2+ caveats above)testImplementationandroidTestImplementation// WRONG — Espresso on testImplementation
dependencies {
testImplementation("androidx.test.espresso:espresso-core:3.7.0")
testImplementation("androidx.test.ext:junit:1.3.0")
}
// WRONG because: Espresso requires a real or simulated Android device. testImplementation
// only puts the JAR on the JVM classpath. The test compiles but ClassNotFoundException /
// IllegalStateException at runtime when Espresso looks for an Instrumentation instance.// RIGHT — Espresso on androidTestImplementation
dependencies {
androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
androidTestImplementation("androidx.test.ext:junit:1.3.0")
}src/androidTest/// WRONG
// src/androidTest/java/com/example/SomeRobolectricTest.kt
@RunWith(RobolectricTestRunner::class)
class SomeRobolectricTest { /* ... */ }
// WRONG because: Robolectric is a JVM Android simulator (per /fundamentals: "Big local test:
// You can use an Android simulator that runs locally, such as Robolectric"). It belongs in
// src/test/. Putting it in src/androidTest/ either fails to find Robolectric (which is on
// testImplementation) or runs it on the device JVM where its shadow mechanism does not work.// RIGHT
// src/test/java/com/example/SomeRobolectricTest.kt
@RunWith(AndroidJUnit4::class) // dispatches to Robolectric on JVM
@Config(sdk = [Build.VERSION_CODES.UPSIDE_DOWN_CAKE])
class SomeRobolectricTest { /* ... */ }ui-test-manifest// WRONG
dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
androidTestImplementation("androidx.compose.ui:ui-test-manifest") // wrong config
}
// WRONG because: ui-test-manifest contributes <activity android:name="ComponentActivity">
// to the merged debug APK manifest. On androidTestImplementation, the merger does not pick
// it up; createComposeRule() crashes with ActivityNotFoundException. Lint also flags this
// as TestManifestGradleConfiguration (WARNING).// RIGHT
dependencies {
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}../../../compose/setup/configuring-test-dependencies/SKILL.mdsharedTest// BROKE on AGP 7.2+
android {
sourceSets {
getByName("test").java.srcDir("src/sharedTest/java")
getByName("androidTest").java.srcDir("src/sharedTest/java")
}
}// MIGRATION OPTION A — extract a :core-testing sub-module
// :core-testing/build.gradle.kts
plugins { id("java-library") }
// classes here are consumed by both test sourcesets in dependent modules
// :feature:home/build.gradle.kts
dependencies {
testImplementation(project(":core-testing"))
androidTestImplementation(project(":core-testing"))
}// MIGRATION OPTION B — testFixtures
android {
testFixtures { enable = true }
}
// src/testFixtures/java/... contains FakeUserRepository, MainDispatcherRule
// dependent module
dependencies {
testImplementation(testFixtures(project(":core")))
androidTestImplementation(testFixtures(project(":core")))
}// MIGRATION OPTION C — KMP source-set naming
// In a KMP Android module:
kotlin {
androidTarget {
compilations.getByName("debug") { /* ... */ }
}
sourceSets {
val androidHostTest by getting // Robolectric, JVM
val androidDeviceTest by getting // emulator/device
val androidCommonTest by getting // helpers shared by both
androidHostTest.dependsOn(androidCommonTest)
androidDeviceTest.dependsOn(androidCommonTest)
}
}plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.feature.home"
compileSdk = 35
defaultConfig {
minSdk = 23
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests.isIncludeAndroidResources = true // required for Robolectric
animationsDisabled = true // hermetic instrumented tests
}
}
dependencies {
// production
implementation("androidx.activity:activity-compose")
// host (JVM) tests — src/test/
testImplementation("junit:junit:4.13.2")
testImplementation("androidx.test:core:1.7.0")
testImplementation("androidx.test.ext:junit:1.3.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.0")
testImplementation("org.mockito:mockito-core:5.13.0")
testImplementation("org.mockito.kotlin:mockito-kotlin:5.4.0")
testImplementation("org.robolectric:robolectric:4.13")
testImplementation("app.cash.turbine:turbine:1.1.0")
// instrumented (device) tests — src/androidTest/
androidTestImplementation("androidx.test.ext:junit:1.3.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
// debug-only — merged manifest entries for test scaffolds
debugImplementation("androidx.compose.ui:ui-test-manifest")
debugImplementation("androidx.fragment:fragment-testing-manifest:1.8.5")
// orchestrator (note: androidTestUtil, not androidTestImplementation)
androidTestUtil("androidx.test:orchestrator:1.6.1")
}src/test/testImplementationsrc/test/src/androidTest/androidTestImplementationandroidx.compose.ui:ui-test-manifestandroidx.fragment:fragment-testing-manifestdebugImplementationandroidTestImplementationdeveloper.android.com/training/testing/...src/sharedTest/tasks/research/R8-android-fundamentals.mdui-test-junit4testImplementationInstrumentation:core-testingandroid.testFixtures.enable = truesharedTestandroidCommonTestandroidHostTestandroidDeviceTestsrc/test/src/androidTest//training/testing/instrumented-testssrc/androidTest/find src/androidTest -name '*.kt' -exec grep -l 'org.robolectric' {} +src/test/find src/test -name '*.kt' -exec grep -l 'androidx.test.espresso\|androidx.test.uiautomator' {} +androidx.compose.ui:ui-test-manifestdebugImplementation./gradlew :<module>:lintTestManifestGradleConfiguration./gradlew :<module>:testDebugUnitTestconnectedDebugAndroidTest./gradlew :<module>:connectedDebugAndroidTestsrc/androidTest/--info:core-testingtestFixturesandroidCommonTestsrc/sharedTest/sourceSets { ... srcDir(...) }testOptions.unitTests.isIncludeAndroidResources = trueR.*developer.android.com/training/testing/local-testssrc/test/testImplementationdeveloper.android.com/training/testing/instrumented-testssrc/androidTest/androidTestImplementationdeveloper.android.com/training/testing/fundamentalstasks/research/R8-android-fundamentals.mdsrc/sharedTest//training/testing/...sharedTesttestFixturesandroidHostTestandroidDeviceTestcompose/ui/ui-test/src/androidHostTest/kotlin/androidx/compose/ui/test/RobolectricComposeTest.ktcompose/material3/material3/src/androidDeviceTest/.../SwitchTest.kt../../concepts/understanding-the-testing-pyramid/SKILL.md../../concepts/choosing-what-to-test/SKILL.md../../doubles/picking-test-doubles/SKILL.md../applying-testing-strategies/SKILL.md../../../compose/setup/configuring-test-dependencies/SKILL.md../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../adb/tests/running-instrumented-tests-via-adb/SKILL.md