0Pricing
Kotlin Multiplatform Academy · Lektion

Suspend-Funktionen und Flows testen

Verwenden Sie runTest und Turbine, um asynchrones Verhalten zu prüfen.

Suspend-Funktionen und Flows testen ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Suspend-Funktionen und Flows testen“ kostenlos?

Ja — der vollständige Text von „Suspend-Funktionen und Flows testen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Suspend-Funktionen und Flows testen“?

Verwenden Sie runTest und Turbine, um asynchrones Verhalten zu prüfen. Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?

Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Suspend-Funktionen und Flows testen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?

Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. kotlin.test in commonTest
  2. Suspend-Funktionen und Flows testen
  3. Ktor-Client und Repositories faken
  4. Tests auf Android- und iOS-Targets ausführen
← Zurück zu Kotlin Multiplatform Academy