Loading...
Loading...
Use this skill to pick which behaviors to cover in an Android test suite using Google's five-category state vocabulary plus the explicit "what NOT to test" list from `/training/testing/fundamentals/what-to-test`. Covers state on screen, state held in memory (ViewModels), persisted state (DB / DataStore / files), other state (system bars / system services), and errors / edge cases. Includes Google's verbatim "Tests to Avoid" list (framework entry points such as activities / fragments / services should not have business logic) and an edge-case mining checklist. Use when the user asks "what should I write tests for", "should I unit-test this Activity", "do I need a test for this util class", "what edge cases am I missing", or "should I test the ViewModel or the Composable".
npx skill4agent add skydoves/android-testing-skills choosing-what-to-test/training/testing/fundamentals/what-to-testActivityFragmentService../understanding-the-testing-pyramid/SKILL.md../../doubles/picking-test-doubles/SKILL.mdsrc/test/src/androidTest/../../strategies/organizing-test-source-sets/SKILL.mdViewModel../understanding-the-testing-pyramid/SKILL.mddeveloper.android.com/training/testing/fundamentals/what-to-test"Screen UI tests check critical user interactions in a single screen. They perform actions such as clicking on buttons, typing in forms, and checking visible states. One test class per screen is a good starting point." —developer.android.com/training/testing/fundamentals/what-to-test
"User flow tests or Navigation tests, covering most common paths. These tests simulate a user moving through a navigation flow." —developer.android.com/training/testing/fundamentals/what-to-test
onNodeWithTag(...).assertIsDisplayed()assertTextEquals(...)assertIsEnabled()onView(...).check(matches(isDisplayed()))../../../instrumentation/espresso/writing-espresso-tests/SKILL.mdViewModelViewModel.uiState"Unit tests for ViewModels, or presenters." —developer.android.com/training/testing/fundamentals/what-to-test
StateFlowLiveData../../../jvm-tests/coroutines/testing-coroutines-with-runtest/SKILL.md../../../jvm-tests/coroutines/testing-flows-with-turbine/SKILL.md"Unit tests for the data layer, especially repositories. Most of the data layer should be platform-independent. Doing so enables test doubles to replace database modules and remote data sources in tests." —developer.android.com/training/testing/fundamentals/what-to-test
/fundamentals/what-to-test/fundamentals"Unit tests should focus on both normal and edge cases. Edge cases are uncommon scenarios that human testers and larger tests are unlikely to catch. Examples include the following:
- Math operations using negative numbers, zero, and boundary conditions.
- All the possible network connection errors.
- Corrupted data, such as malformed JSON.
- Simulating full storage when saving to a file.
- Object recreated in the middle of a process (such as an activity when the device is rotated)." —
developer.android.com/training/testing/fundamentals/what-to-test
"Some unit tests should be avoided because of their low value:
- Tests that verify the correct operation of the framework or a library, not your code.
- Framework entry points such as activities, fragments, or services should not have business logic so unit testing shouldn't be a priority. Unit tests for activities have little value, because they would cover mostly framework code and they require a more involved setup. Instrumented tests such as UI tests can cover these classes." —
developer.android.com/training/testing/fundamentals/what-to-test
RobolectricActivityonCreateRoomDatabase.BuilderRetrofit@GETActivityViewModelUseCaseRepository/fundamentals"There are more specialized tests such as screenshot tests, performance tests, and monkey tests." —developer.android.com/training/testing/fundamentals/what-to-test
../../../compose/audit/auditing-compose-test-suite/SKILL.md// WRONG
@RunWith(AndroidJUnit4::class)
class CheckoutActivityTest {
@get:Rule val rule = ActivityScenarioRule(CheckoutActivity::class.java)
@Test fun whenTotalIsNegative_showsError() {
rule.scenario.onActivity { activity ->
activity.viewModel.applyCoupon("FREE_BEER_NEGATIVE_PRICE")
// assert on the activity's TextView contents
assertEquals("Error", activity.findViewById<TextView>(R.id.error).text)
}
}
}
// WRONG because: the test rebuilds the framework lifecycle, the Activity, the inflater,
// and the View graph just to assert business logic that lives in the ViewModel. Per
// /what-to-test: "Unit tests for activities have little value, because they would cover
// mostly framework code".// RIGHT — small, JVM, sub-50 ms
class CheckoutViewModelTest {
@Test fun applyCoupon_negativePrice_emitsError() = runTest {
val vm = CheckoutViewModel(FakePricingRepository(allowNegative = true))
vm.applyCoupon("FREE_BEER_NEGATIVE_PRICE")
assertEquals(CheckoutUiState.Error("Negative total"), vm.uiState.value)
}
}Activity// WRONG
class FormatDurationTest {
private val clock = mockk<Clock>()
private val locale = mockk<Locale>()
@Test fun formatsMinutesAndSeconds() {
every { clock.now() } returns Instant.parse("2024-01-01T00:01:30Z")
every { locale.language } returns "en"
assertEquals("1m 30s", formatDuration(90.seconds, clock, locale))
}
}
// WRONG because: this is a pure-function utility per /what-to-test ("Unit tests for
// utility classes such as string manipulation and math"). Mocks add ceremony with
// no value. Pass real values.// RIGHT
class FormatDurationTest {
@Test fun formatsMinutesAndSeconds() {
assertEquals("1m 30s", formatDuration(90.seconds))
}
@Test fun handlesZero() {
assertEquals("0s", formatDuration(0.seconds))
}
@Test fun handlesHoursBoundary() {
assertEquals("1h 0m 0s", formatDuration(3600.seconds))
}
}// WRONG — only the happy path
class ParseAmountTest {
@Test fun parsesValidAmount() = assertEquals(12.34, parseAmount("12.34"))
}// RIGHT — edge cases per /what-to-test
class ParseAmountTest {
@Test fun parsesValidAmount() = assertEquals(12.34, parseAmount("12.34"))
@Test fun parsesZero() = assertEquals(0.0, parseAmount("0"))
@Test fun parsesNegative() = assertEquals(-5.0, parseAmount("-5"))
@Test fun parsesScientific() = assertEquals(1e6, parseAmount("1e6"))
@Test fun rejectsEmpty() = assertNull(parseAmount(""))
@Test fun rejectsMalformed() = assertNull(parseAmount("12.34.56"))
@Test fun rejectsLeadingWhitespace() = assertNull(parseAmount(" 12"))
@Test fun rejectsLocaleComma() = assertNull(parseAmount("12,34")) // German format
@Test fun handlesMaxDouble() = assertEquals(Double.MAX_VALUE, parseAmount("1.7976931348623157E308"))
}/training/testing/fundamentals/what-to-testInt.MAX_VALUELong.MIN_VALUEDouble.NaNDouble.POSITIVE_INFINITYemptyList()listOf(x)coerceInputValues12,34٠١٢IOExceptionrememberSaveableSavedStateHandlekotlinx-coroutines-testWhat does the code touch? Test? Where?
-----------------------------------------------------------------------------
Pure function / utility (string, math, mapping) YES src/test/ (small)
ViewModel / presenter state YES src/test/ (small)
Repository / use case (fake the storage) YES src/test/ (small)
Real Room / DataStore YES src/androidTest/ (small instrumented)
Composable rendering of state YES src/androidTest/ or src/test/ (Robolectric)
Navigation flow (multi-screen) YES src/androidTest/ (big)
Activity onCreate / inflation logic NO framework code
Fragment onViewCreated wiring NO framework code
Service onStartCommand wiring NO framework code
Retrofit interface (annotation routing) NO library code
Hilt module declarations NO library code
Compose Modifier internals NO library code
Generated code NO generated
Private function with no public observable effect NO test the public callerViewModelRepositoryActivityFragmentService/what-to-testViewModelRepositoryassertIsDisplayedcheck(matches(isDisplayed()))ActivityFragmentServiceRetrofitRoomHiltComposedeveloper.android.com/training/testing/fundamentals/what-to-testdeveloper.android.com/training/testing/fundamentalsdeveloper.android.com/training/testing/local-testsdeveloper.android.com/training/testing/instrumented-teststasks/research/R8-android-fundamentals.md../understanding-the-testing-pyramid/SKILL.md../../doubles/picking-test-doubles/SKILL.md../../strategies/applying-testing-strategies/SKILL.md../../strategies/organizing-test-source-sets/SKILL.md../../../jvm-tests/coroutines/testing-coroutines-with-runtest/SKILL.md../../../jvm-tests/coroutines/testing-flows-with-turbine/SKILL.md../../../jvm-tests/mocking/mocking-with-mockito/SKILL.md../../../jvm-tests/mocking/mocking-with-mockk/SKILL.md../../../instrumentation/espresso/writing-espresso-tests/SKILL.md../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md../../../instrumentation/scenarios/launching-fragments-with-fragmentscenario/SKILL.md../../../compose/audit/auditing-compose-test-suite/SKILL.md