kotlin-testing-kmp
Use when designing, implementing, or reviewing tests in KMP projects — unit tests, instrumented tests, Compose Multiplatform UI tests, test doubles, test strategy, stability, performance, and screenshot testing.
mmiani · 已有 AI 审核记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add mmiani/kotlin-kmp-claude-agent-skills --skill kotlin-testing-kmp技能说明
name: kotlin-testing-kmp description: Use when designing, implementing, or reviewing tests in KMP projects — unit tests, instrumented tests, Compose Multiplatform UI tests, test doubles, test strategy, stability, performance, and screenshot testing. allowed-tools: Read, Grep, Glob license: Apache-2.0 metadata: author: Mariano Miani version: "1.2.0"
Kotlin Multiplatform Testing
Use this skill when designing, implementing, or reviewing tests in a Kotlin Multiplatform project.
This skill is intentionally strict. Its purpose is to keep the test suite fast, trustworthy, behavior-focused, and aligned with Android’s testing guidance while still respecting shared KMP boundaries and Compose Multiplatform testing patterns.
Primary goals
The testing strategy should optimize for:
- strong confidence in business logic
- fast feedback from local/unit tests
- clear separation between local, Robolectric, instrumented, and UI tests
- behavior-focused tests instead of implementation-detail tests
- appropriate use of test doubles
- stable and performant larger tests
- isolated testing of shared KMP logic
- practical Compose Multiplatform UI test coverage
- targeted screenshot testing where visual regressions matter
Do not optimize for raw coverage percentages alone. Optimize for confidence, speed, signal quality, and maintainability.
Official defaults to prefer
Unless the project has a strong reason not to, prefer these defaults:
- many small local/unit tests
- fewer integration tests
- fewer end-to-end/UI tests than lower-level tests
- tests focused on user-visible behavior and business outcomes
- use of test doubles to isolate dependencies
kotlin.testfor assertions and test declarations in shared KMP test source sets — this is the cross-platform test API recommended by JetBrains and works across JVM, native, and JS/Wasm targets- local tests in
src/test(JVM-side tests in Android modules) - Android instrumented tests in
src/androidTest - Robolectric only when Android framework behavior is needed but device execution is not necessary
- common Compose Multiplatform UI tests where the behavior is truly shared
- stable UI tests with clear synchronization and controlled environment setup
- screenshot tests for important visual surfaces when appearance regressions matter
Test strategy defaults
1. Testing pyramid
Prefer a pyramid-shaped suite:
- many unit tests
- fewer integration tests
- fewer UI/end-to-end tests
Review expectation:
- lower-level tests should catch most regressions
- larger tests should validate integration and user workflows, not replace unit coverage
Flag as a concern when:
- most confidence depends on slow UI tests
- business logic is only exercised through end-to-end flows
- the suite is top-heavy and expensive to run
2. Behavior over implementation detail
Prefer tests that validate:
- observable outputs
- state transitions
- user-visible effects
- business rules
Be cautious with tests that lock down:
- internal private structure
- implementation-specific sequencing that users do not observe
- framework internals
Flag as a concern when:
- tests fail after harmless refactors
- mocks assert incidental calls instead of real behavior
- UI tests are verifying widget internals rather than actual interaction outcomes
Test-scope review dimensions
3. What should be tested first
Prioritize tests for:
- business rules
- domain/use-case behavior
- repository coordination logic
- DTO/domain/UI mapping
- state-holder transitions
- failure and retry handling
- navigation decision logic where important
- shared UI behavior that is meaningful across targets
Flag as a concern when:
- trivial pass-through code is heavily tested while important rules are not
- mapping and error paths are untested
- state transitions are inferred rather than verified
4. Local unit tests
Local tests should be the default choice for fast feedback.
For shared KMP modules, tests in commonTest (using kotlin.test) run on all declared targets — JVM, native, JS/Wasm — making them the correct layer for shared business logic. Do not confuse commonTest (shared KMP tests) with src/test (Android/JVM-local tests).
Check whether:
- pure Kotlin/shared logic is tested in
commonTestusingkotlin.test, not pushed into Android-specific test layers - Android-specific behavior is tested in
src/test(local JVM) orsrc/androidTest(instrumented), not in shared test source sets - test setup avoids Android device/emulator dependency when unnecessary
- most business logic tests live in the lowest practical test layer
Review expectations:
- shared logic in KMP should be exercised in
commonTestfirst, before platform-specific layers src/testremains the main home for fast-running JVM-side tests on Android-only modules- local tests are preferred unless the behavior truly needs Android runtime support
Flag as a concern when:
- shared business logic is only tested in Android-specific layers (
src/androidTest) without reason - the suite pays emulator/device cost for logic that could be a
commonTestunit test kotlin.testis not used in shared source sets — shared tests depend on JUnit directly, breaking non-JVM targets
4a. kotlin.test for shared source sets
In KMP projects, shared test source sets (e.g., commonTest) should use kotlin.test for assertions and test structure. kotlin.test provides @Test, assertEquals, assertNotNull, assertFailsWith, and other essentials that compile correctly for all KMP targets (JVM, native, JS, Wasm).
Check whether:
- shared test code uses
kotlin.testrather than JUnit or platform-specific assertion libraries kotlin.testis declared as a dependency in thecommonTestsource set- platform-specific test libraries (JUnit, XCTest wrappers) are added only in platform-specific test source sets when needed
Flag as a concern when:
- JUnit annotations or assertions appear in `com