$npx -y skills add hanamizuki/solopreneur --skill testingComprehensive testing strategy involving Unit, Integration, Hilt, and Screenshot tests.
| 1 | # Android Testing Strategies |
| 2 | |
| 3 | This skill provides expert guidance on testing modern Android applications, inspired by "Now in Android". It covers **Unit Tests**, **Hilt Integration Tests**, and **Screenshot Testing**. |
| 4 | |
| 5 | ## Testing Pyramid |
| 6 | |
| 7 | 1. **Unit Tests**: Fast, isolate logic (ViewModels, Repositories). |
| 8 | 2. **Integration Tests**: Test interactions (Room DAOs, Retrofit vs MockWebServer). |
| 9 | 3. **UI/Screenshot Tests**: Verify UI correctness (Compose). |
| 10 | |
| 11 | ## Dependencies (`libs.versions.toml`) |
| 12 | |
| 13 | Ensure you have the right testing dependencies. |
| 14 | |
| 15 | ```toml |
| 16 | [libraries] |
| 17 | junit4 = { module = "junit:junit", version = "4.13.2" } |
| 18 | kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" } |
| 19 | androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version = "1.1.5" } |
| 20 | espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version = "3.5.1" } |
| 21 | compose-ui-test = { group = "androidx.compose.ui", name = "ui-test-junit4" } |
| 22 | hilt-android-testing = { group = "com.google.dagger", name = "hilt-android-testing", version.ref = "hilt" } |
| 23 | roborazzi = { group = "io.github.takahirom.roborazzi", name = "roborazzi", version.ref = "roborazzi" } |
| 24 | ``` |
| 25 | |
| 26 | ## Screenshot Testing with Roborazzi |
| 27 | |
| 28 | Screenshot tests ensure your UI doesn't regress visually. NiA uses **Roborazzi** because it runs on the JVM (fast) without needing an emulator. |
| 29 | |
| 30 | ### Setup |
| 31 | |
| 32 | 1. Add the plugin to `libs.versions.toml`: |
| 33 | ```toml |
| 34 | [plugins] |
| 35 | roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" } |
| 36 | ``` |
| 37 | 2. Apply it in your module's `build.gradle.kts`: |
| 38 | ```kotlin |
| 39 | plugins { |
| 40 | alias(libs.plugins.roborazzi) |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | ### Writing a Screenshot Test |
| 45 | |
| 46 | ```kotlin |
| 47 | @RunWith(AndroidJUnit4::class) |
| 48 | @GraphicsMode(GraphicsMode.Mode.NATIVE) |
| 49 | @Config(sdk = [33], qualifiers = RobolectricDeviceQualifiers.Pixel5) |
| 50 | class MyScreenScreenshotTest { |
| 51 | |
| 52 | @get:Rule |
| 53 | val composeTestRule = createAndroidComposeRule<ComponentActivity>() |
| 54 | |
| 55 | @Test |
| 56 | fun captureMyScreen() { |
| 57 | composeTestRule.setContent { |
| 58 | MyTheme { |
| 59 | MyScreen() |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | composeTestRule.onRoot() |
| 64 | .captureRoboImage() |
| 65 | } |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | ## Hilt Testing |
| 70 | |
| 71 | Use `HiltAndroidRule` to inject dependencies in tests. |
| 72 | |
| 73 | ```kotlin |
| 74 | @HiltAndroidTest |
| 75 | class MyDaoTest { |
| 76 | |
| 77 | @get:Rule |
| 78 | var hiltRule = HiltAndroidRule(this) |
| 79 | |
| 80 | @Inject |
| 81 | lateinit var database: MyDatabase |
| 82 | private lateinit var dao: MyDao |
| 83 | |
| 84 | @Before |
| 85 | fun init() { |
| 86 | hiltRule.inject() |
| 87 | dao = database.myDao() |
| 88 | } |
| 89 | |
| 90 | // ... tests |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | ## Running Tests |
| 95 | |
| 96 | * **Unit**: `./gradlew test` |
| 97 | * **Screenshots**: `./gradlew recordRoborazziDebug` (to record) / `./gradlew verifyRoborazziDebug` (to verify) |