testing-setup
Installation
SKILL.md
Step 1: Analyze the current testing setup
To understand the testing setup of an existing project, look for these key dependencies in the project's configuration (e.g., libs.versions.toml or build.gradle):
- Dependency Injection: Identify the framework (e.g., Hilt, Koin, vanilla Dagger).
- Unit Testing: Identify the core framework (JUnit 4 or JUnit 5).
- Mocking/Fakes: Identify the preferred approach (see Manual Fakes First in Step 2).
- Coroutine & Flow Testing: Check for utilities like
kotlinx-coroutines-testandTurbine. - Screenshot Testing: Check whether Roborazzi is declared (only expected in a
:design-systemmodule). - Environment: Ensure the JDK and AGP versions are compatible with the project's
compileSdkand testing tools (seedependency-manager).
Step 2: Set up the Testing Infrastructure
Configure the chosen frameworks following their official guidelines. Ensure that:
- Testing dependencies are correctly scoped (
testImplementationfor pure JVM tests). Do NOT declareandroidTestImplementationdependencies (Espresso,ui-test-junit4, UI Automator) unless a module explicitly needs them under Step 5 (Design System exception). - A clear boundary exists between production and test code.
MainDispatcherRule: Every module that tests ViewModels (or any code usingviewModelScope/stateIn) MUST provide a single reusable JUnit rule that callsDispatchers.setMain(UnconfinedTestDispatcher())instarting()andDispatchers.resetMain()infinished(). Without it, pure JVM tests fail withModule with the Main dispatcher had failed to initialize.- Manual Fakes First: When a domain interface exists (repository, data source, API service), write a hand-made
Fake*implementation (in-memory, deterministic) instead of using a mocking framework (MockK / Mockito). Fakes are faster, have no bytecode-instrumentation cost, and do not couple tests to implementation details (coVerify-style assertions). Mocking frameworks are only acceptable for types that cannot be faked (final platform classes) and only when explicitly justified.