Skip to content
Back to skills

Kmp Testing Patterns

ASecurity

Use when writing or fixing unit tests in a Kotlin Multiplatform project - commonTest, kotlin.test, runTest, StandardTestDispatcher, Dispatchers.setMain, Turbine, testing a ViewModel's StateFlow and one-shot effects, fakes versus mocks (MockK fails on iOS, Mokkery), Ktor MockEngine, Koin test overrides with or without the Koin Compiler Plugin, XCTest or Swift Testing in the iOS host app, or making new tests run the same on the JVM and on iOS. Not for Compose UI tests or Android-only test setup.

  • 17 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
developmentgoswiftkotlinbashnodeawstestinggitapi

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned December 4, 2026

npx -y skills add Kushpawan/kmp-mobile-skills --skill kmp-testing-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Kmp Testing Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Kmp Testing Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/kushpawan-kmp-testing-patterns/badge)](https://www.skillsdirectory.com/skills/kushpawan-kmp-testing-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: kmp-testing-patterns
description: Use when writing or fixing unit tests in a Kotlin Multiplatform project - commonTest, kotlin.test, runTest, StandardTestDispatcher, Dispatchers.setMain, Turbine, testing a ViewModel's StateFlow and one-shot effects, fakes versus mocks (MockK fails on iOS, Mokkery), Ktor MockEngine, Koin test overrides with or without the Koin Compiler Plugin, XCTest or Swift Testing in the iOS host app, or making new tests run the same on the JVM and on iOS. Not for Compose UI tests or Android-only test setup.
license: Apache-2.0
compatibility: Kotlin Multiplatform Gradle project with commonTest. Running iOS tests needs macOS with Xcode and an iOS simulator runtime; the Swift 6.4 test interop notes assume Xcode 27.
metadata:
  category: testing
  platforms: android, ios
---

# KMP testing patterns

Business logic in `commonMain` is tested once, in `commonTest`, and runs on every target: JVM,
Android host and the iOS simulator. A reliable test controls time and threads with test dispatchers,
subscribes to effects before it acts, asserts both state and effects, and uses hand-written fakes
that run on every target.

## When to use

- Writing tests for a ViewModel, use case, repository, mapper, reducer or validator in a KMP module.
- A test you are writing hangs, misses an emission, or needs dispatcher control.
- Choosing between fakes and mocks, or setting up Turbine, `MockEngine` or Koin for tests.
- Deciding which Gradle task runs which tests.

## When not to use

- Raising a module's coverage number in a loop: use `kmp-test-coverage-backfill`, which applies
  these patterns.
- Checking that the whole repo builds and passes on both platforms: use `kmp-build-check`.
- Compose UI tests (`runComposeUiTest`, `onNodeWithTag`): use `compose-multiplatform-ui-testing`.
  Screenshot tests: `kmp-screenshot-testing`. End-to-end flows: `kmp-maestro-e2e`. Locators for any
  of them: `mobile-accessibility-test-ids`.
- Koin graph checks and `[KOIN-…]` build errors: use `kmp-koin-di`.
- Test infrastructure for an Android-only app or the Android host app: Google's
  [`testing-setup`](https://github.com/android/skills/tree/main/testing/testing-setup) skill.
- Diagnosing an existing test that passes and fails without code changes, or fails only in CI or
  only on iOS: use `kmp-flaky-test-triage`.

## Step 0 - Read the project first

Before writing a test, read two or three existing tests and the class under test. Write in the
project's idiom, not this skill's defaults.

| Find out | Where to look | Follow it |
|---|---|---|
| Test names | existing `*Test.kt` | default `should_<expected>_when_<condition>`; keep the project's own scheme if it has one |
| Assertion library | imports | `kotlin.test`, AssertK, Kotest assertions: whichever is there |
| Flow testing | imports of `app.cash.turbine` | Turbine if present; otherwise manual collection |
| Fakes | `commonTest/**/Fake*.kt`, `*Fakes.kt`, `InMemory*` in `commonMain` | reuse before writing new ones |
| Mocking | `dev.mokkery`, `io.mockk` in the build files | do not add a mocking library that is not already there; a Mokkery version must match the Kotlin version |
| Dispatchers | a `DispatcherProvider` (or similar) on constructors | inject a test dispatcher through it |
| ViewModel base | `ViewModel`, or a project base such as `MviViewModel<State, Effect>` | test through its public state/effect API |
| DI | Koin, kotlin-inject, Metro, manual; for Koin, the `io.insert-koin.compiler.plugin` plugin | construct directly in unit tests; use DI only for wiring tests. With the Koin Compiler Plugin, the build checks the graph: no `verify()` tests |
| Source sets / tasks | `build.gradle.kts`, `./gradlew :m:tasks --all` | see the Quick reference |
| Versions | `gradle/libs.versions.toml` | add test libraries there, never inline |
| iOS host tests | a `*Tests` target in `iosApp/`; `import XCTest` or `import Testing` | write Swift tests in the framework the target already uses |

## Workflow

1. **Choose what to test.** Test everything that has behaviour:
   - ViewModels and state holders: every action, every resulting state, every emitted effect;
   - use cases;
   - repositories (mapping, caching, error handling);
   - mappers (nullable and unknown input);
   - reducers, validators and state machines.

   Do **not** unit-test Composables, generated code, DI modules or plain DTOs by default. Put UI
   behaviour in a small number of Compose UI tests, written with the
   `compose-multiplatform-ui-testing` skill.
   *Done when* you have a list of behaviours, one test per behaviour.

2. **Put the test in the right source set.** `commonTest` for anything in `commonMain`. Use
   `androidHostTest` / `androidUnitTest` / `jvmTest` / `iosTest` only for `actual` implementations
   and platform code. Use the same package as the class under test, so `internal` members are visible.

3. **Build the test doubles.** Prefer hand-written fakes: an implementation of the interface with
   settable results, call counters and recorded arguments.
   - Fakes run on every target. MockK and Mockito need the JVM, so they fail iOS test compilation.
   - [Mokkery](https://mokkery.dev) 3.x is the KMP mocking compiler plugin. Use it only if the
     project already does, and keep its version matched to Kotlin: 3.6.0 for Kotlin 2.4.x, 3.7.0
     for Kotlin 2.5.0 (mokkery.dev compatibility table, December 2026).
   - Reuse in-memory production implementations as fakes.

4. **Control coroutines.**
   - Wrap every suspend test in `runTest { }`. Delays use virtual time, so a 30-second timeout
     finishes instantly.
   - Inject dispatchers through a `DispatcherProvider` and pass test dispatchers that share
     `testScheduler`. A `withContext(Dispatchers.IO)` hard-coded in production escapes the scheduler,
     so `advanceUntilIdle()` cannot drive it.
   - Anything on `viewModelScope` or `Dispatchers.Main` needs `Dispatchers.setMain(testDispatcher)` in
     `@BeforeTest` and `Dispatchers.resetMain()` in `@AfterTest`. JVM tests have no Main dispatcher.
     In an iOS test binary, work posted to the main queue may never run.
   - Pick the dispatcher on purpose:

     | Dispatcher | Behaviour | Use when |
     |---|---|---|
     | `StandardTestDispatcher` | queues work until `runCurrent()` / `advanceUntilIdle()` / `advanceTimeBy()` | you assert intermediate states or ordering |
     | `UnconfinedTestDispatcher` | runs launched work eagerly | the ViewModel loads in `init`, or for effect collectors |

   - Never wait with `delay()` in the test body, `Thread.sleep`, or real-time polling.

5. **Subscribe to effects before you act.** One-shot effects (navigate, toast, dialog) are lost or
   delayed when nobody is collecting. A `MutableSharedFlow(replay = 0)` drops an emission that has no
   subscriber. Start the collector first:

   ```kotlin
   val effects = mutableListOf<NotesEffect>()
   backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { viewModel.effects.toList(effects) }
   viewModel.onAction(NotesAction.Open(id = "42"))      // act AFTER subscribing
   advanceUntilIdle()
   ```

   Or, with Turbine: `turbineScope { val e = viewModel.effects.testIn(backgroundScope); ... }`.
   `backgroundScope` is cancelled when the test ends, so an endless collector does not hang `runTest`.

6. **Assert state and effects, every time.** Compare the whole `UiState` with `assertEquals`, so an
   unexpected field change fails the test. Assert the exact effect list, `emptyList()` included, so
   an extra navigation fails too. For an in-flight state (loading), hold the fake on a
   `CompletableDeferred` gate and call `runCurrent()`. `StateFlow` conflates, so without a gate a fast
   fake skips straight past loading.

7. **Keep each test independent.** Use Arrange-Act-Assert and one behaviour per test. Create fakes per
   test or in `@BeforeTest`. Reset every global in `@AfterTest`: `Dispatchers.resetMain()`,
   `stopKoin()`, and any singleton you configured. Tests must pass alone, in any order, and on every
   target.

8. **Run the tests on the JVM and on iOS** (Quick reference). A test that passes only on the JVM is
   not done.

9. **Swift-side code gets Swift tests, nothing more.** Only Swift wrappers, adapters and startup glue
   in the iOS host app need XCTest or Swift Testing tests. Shared logic stays in `commonTest`. With
   Swift 6.4 (Xcode 27), XCTest and Swift Testing assertions work inside each other's tests (ST-0021),
   so a target can migrate one test at a time, and failures that were silently ignored before now
   fail. Details: [references/ios-host-tests.md](references/ios-host-tests.md).

A complete feature, worked through in three parts:

- [references/viewmodel-example-code.md](references/viewmodel-example-code.md): the ViewModel,
  dispatcher provider and hand-written fake;
- [references/viewmodel-example-tests.md](references/viewmodel-example-tests.md): seven tests, and
  why each choice was made;
- [references/turbine-variants.md](references/turbine-variants.md): the same assertions with Turbine.

`MockEngine`, repository, Koin and Mokkery patterns are in
[references/data-and-di-tests.md](references/data-and-di-tests.md).

## Patterns at a glance

```kotlin
@OptIn(ExperimentalCoroutinesApi::class)
class NotesViewModelTest {
    private val testDispatcher = StandardTestDispatcher()
    private val notesRepository = FakeNotesRepository()

    @BeforeTest fun setUp() = Dispatchers.setMain(testDispatcher)
    @AfterTest fun tearDown() = Dispatchers.resetMain()

    @Test
    fun should_keepNoteAndShowMessage_when_deleteFails() = runTest {
        // Arrange
        notesRepository.notes = listOf(note("1"))
        notesRepository.deleteSucceeds = false
        val viewModel = NotesViewModel(notesRepository, TestDispatcherProvider(testDispatcher))
        viewModel.onAction(NotesAction.Refresh)
        advanceUntilIdle()
        val effects = mutableListOf<NotesEffect>()
        backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { viewModel.effects.toList(effects) }

        // Act
        viewModel.onAction(NotesAction.Delete(id = "1"))
        advanceUntilIdle()

        // Assert: state AND effects
        assertEquals(listOf(note("1")), viewModel.state.value.notes)
        assertEquals(listOf<NotesEffect>(NotesEffect.ShowMessage("Could not delete the note")), effects)
    }
}
```

## Quick reference

Which Gradle task runs which tests. Task names depend on the plugins, so confirm them with
`./gradlew :<module>:tasks --all`.

| Task | Runs | Notes |
|---|---|---|
| `:m:allTests` | every target's tests, plus an aggregated report | includes iOS simulator tests on macOS |
| `:m:testDebugUnitTest` | `commonTest` + `androidUnitTest` on the JVM | modules on `com.android.library` / `application` with `androidTarget()` |
| `:m:testAndroidHostTest` | `commonTest` + `androidHostTest` on the JVM | modules on `com.android.kotlin.multiplatform.library`. Exists only after `withHostTest {}` in its `android {}` block; host tests are off by default. |
| `:m:jvmTest` | `commonTest` + `jvmTest` | modules with a `jvm()` target |
| `:m:iosSimulatorArm64Test` | `commonTest` + `iosTest` on the iOS simulator | Apple-silicon Macs; `iosX64Test` on Intel. Device targets cannot run tests on the Mac. |
| `:m:connectedAndroidTest` (or `connectedDebugAndroidTest`) | device or emulator tests | needs a running emulator or device |

Run one class: `./gradlew :shared:jvmTest --tests "com.example.app.notes.NotesViewModelTest"`. The
same `--tests` filter works for the Android unit-test and iOS simulator test tasks.

| API | Use |
|---|---|
| `runTest { }` | every suspend test; virtual time |
| `testScheduler`, `backgroundScope` | inside `runTest`: share the scheduler; host endless collectors |
| `runCurrent()` / `advanceUntilIdle()` / `advanceTimeBy(ms)` | run due tasks / run everything / move virtual time. After `advanceTimeBy`, call `runCurrent()` for tasks due exactly then. |
| `Dispatchers.setMain(d)` / `Dispatchers.resetMain()` | replace Main for `viewModelScope` |
| Turbine `flow.test { awaitItem() }` | assert a sequence of emissions; 3 s wall-clock timeout by default |
| Turbine `turbineScope { flow.testIn(backgroundScope) }` | several flows at once (state plus effects) |
| Ktor `MockEngine { respond(...) }` + `engine.requestHistory` | API-layer tests without a network |
| `startKoin { modules(prod, testOverrides) }` / `stopKoin()` | wiring tests; the last module wins |
| Koin graph check | the build, with the Koin Compiler Plugin; a JVM `verify()` test without it |

## Verify

```bash
./gradlew :shared:<jvm test task>              # testDebugUnitTest, testAndroidHostTest or jvmTest
./gradlew :shared:iosSimulatorArm64Test        # macOS with Xcode only
./gradlew :shared:allTests                     # everything this host can run
# iOS host app tests, only when you changed Swift code (scheme and simulator from the project):
xcodebuild test -project iosApp/iosApp.xcodeproj -scheme iosApp -destination 'platform=iOS Simulator,name=<simulator>'
```

Report each task as **PASS** (exit 0), **FAIL** (quote the first failing test and its assertion) or
**NOT RUN**, with the reason: for example "iOS tests NOT RUN: no Xcode on this machine". Never call
new tests "passing" when only the JVM half ran.

## Common mistakes

| Mistake | Why it bites | Fix |
|---|---|---|
| No `Dispatchers.setMain` for a ViewModel test | JVM: "Module with the Main dispatcher is missing" (Android: "Method getMainLooper in android.os.Looper not mocked"). iOS: work posted to Main may never run. | `setMain` in `@BeforeTest`, `resetMain` in `@AfterTest` |
| A new `StandardTestDispatcher()` per collaborator | Several schedulers; `advanceUntilIdle()` drives only one | One dispatcher, or `StandardTestDispatcher(testScheduler)` |
| `Dispatchers.IO` hard-coded in production | Work escapes virtual time; the test is flaky or slow | Inject a `DispatcherProvider` |
| Triggering the action, then collecting effects | The emission is gone (`SharedFlow`) or the test races (`Channel`) | Collect in `backgroundScope` first, or Turbine `testIn` |
| Asserting state only | A wrong navigation or toast goes unnoticed | Assert the exact effect list too |
| Asserting a loading state with a fast fake | `StateFlow` conflates; the loading state is never seen | Hold the fake on a `CompletableDeferred`, then `runCurrent()` |
| MockK or Mockito in `commonTest` | JVM-only; iOS test compilation fails | Hand-written fakes (or Mokkery if adopted) |
| `advanceUntilIdle()` with a ticker or polling loop | The loop reschedules forever; the test hangs | `advanceTimeBy(n)` then `runCurrent()` |
| Asserting `HashMap` / `HashSet` iteration order | The order is unspecified and differs between JVM and Native | Compare as sets or maps, or sort first |
| Tests that depend on the system time zone, locale or clock | Pass locally, fail in CI or on the simulator | Inject a clock; use fixed time zones |
| A test MockEngine client built by hand | It tests different plugins and config than production uses | Use the production client factory with the mock engine |
| Forgetting `stopKoin()` | The next `startKoin` throws; definitions leak between tests | `stopKoin()` in `@AfterTest` |
| Only JVM tests run in CI | iOS-only failures ship | Run `iosSimulatorArm64Test` on a macOS runner |
| `verify()` or `checkModules()` tests in a project with the Koin Compiler Plugin | They duplicate the build's graph check; `checkModules()` is deprecated | Rely on the build; see `kmp-koin-di` |
| A Mokkery version that does not match the Kotlin version | Compiler API changes break the plugin; the test build fails | Pick the version from mokkery.dev's compatibility table |
| Porting `commonTest` cases to XCTest or Swift Testing | The same logic is tested twice and drifts | Swift tests cover Swift-side code only |
| `XCTAssert…` inside a Swift Testing `@Test` on Swift before 6.4 | The failure is silently ignored | Use `#expect`; on 6.4, keep the interop mode at `complete` or `strict` |

## Related skills

- `kmp-test-coverage-backfill`: apply these patterns in a measured loop to reach a coverage target.
- `compose-multiplatform-ui-testing`: Compose UI tests with `runComposeUiTest` on every target.
- `kmp-screenshot-testing` and `kmp-maestro-e2e`: screenshot tests and end-to-end flows.
- `kmp-koin-di`: Koin graph checks with and without the Compiler Plugin.
- `kmp-build-check`: run the tests on both platforms, with PASS / FAIL / NOT RUN reporting.
- `mobile-accessibility-test-ids`: stable `testTag`s for Compose UI tests and end-to-end tests.
- `kmp-flaky-test-triage`: when an existing test is flaky or fails only on one platform.
- `compose-multiplatform-screen` and `kmp-data-layer`: the production shapes these tests target.
- [cashapp/turbine](https://github.com/cashapp/turbine): Flow testing library.
- [skydoves/android-testing-skills](https://github.com/skydoves/android-testing-skills): Android testing skills.
- [chrisbanes/skills](https://github.com/chrisbanes/skills): Kotlin coroutines/Flow and Compose skills, including Compose UI testing.
- [AvdLee/Swift-Testing-Agent-Skill](https://github.com/AvdLee/Swift-Testing-Agent-Skill) and
  [twostraws/Swift-Testing-Agent-Skill](https://github.com/twostraws/Swift-Testing-Agent-Skill): Swift
  Testing for the iOS side (MIT, linked).
- [android/skills `testing/testing-setup`](https://github.com/android/skills/tree/main/testing/testing-setup):
  test strategy and harnesses for the Android host app.

Files in this skill

  • SKILL.md13.9 KB
  • references/data-and-di-tests.md8.1 KB
  • references/viewmodel-test-worked-example.md14.4 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.