0Pricing
Kotlin Multiplatform Academy · Lesson

Test suspend Functions & Flows

Use runTest and Turbine to assert async behavior.

Test suspend Functions & Flows is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Async Needs Special Tests

Suspend functions and Flows do not finish instantly, so you need tools that wait for async work before asserting. ⏳

Meet runTest

Wrap async assertions in runTest from the coroutines-test library; it provides a scope and skips virtual delays automatically.

@Test
fun loadsUser() = runTest {
    val u = repo.load(1)
    assertEquals("Sam", u.name)
}

Add the Test Helper

Pull kotlinx-coroutines-test into commonTest so runTest and friends are available on every target.

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")

Awaiting a suspend Result

Inside runTest you simply call the suspend function directly and assert on whatever it returns.

val total = calc.fetchTotal()
assertEquals(42, total)

Time Skips for Free

runTest uses virtual time, so a delay of seconds resolves immediately and your tests stay fast.

Testing a Flow by Hand

You can collect a finite Flow into a list with toList and assert on the emitted values.

val values = numbers().toList()
assertEquals(listOf(1, 2, 3), values)

Why Turbine Helps

For ongoing streams, the Turbine library lets you await items one at a time without manual collection plumbing.

Add Turbine

Drop Turbine into commonTest; it is a tiny multiplatform helper built just for testing Flows.

implementation("app.cash.turbine:turbine:1.1.0")

Await Items with test

Turbine wraps a Flow in a test block where awaitItem returns each emission in order.

flow.test {
    assertEquals(1, awaitItem())
    awaitComplete()
}

Finish the Stream Cleanly

End every Turbine block with awaitComplete or cancel, or the test fails for unconsumed events.

Assert StateFlow Updates

Turbine shines on StateFlow: await the initial value, trigger an action, then await the next emitted state.

Quick Check

Pick the right async test wrapper.

Recap

Use runTest for suspend code and Turbine's test block to await Flow emissions, all from commonTest.

Frequently asked questions

Is the “Test suspend Functions & Flows” lesson free?

Yes — the full text of “Test suspend Functions & Flows” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Test suspend Functions & Flows”?

Use runTest and Turbine to assert async behavior. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Test suspend Functions & Flows” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. kotlin.test in commonTest
  2. Test suspend Functions & Flows
  3. Fake the Ktor Client & Repos
  4. Run Tests on Android & iOS Targets
← Back to Kotlin Multiplatform Academy