suspend関数とFlowをテストする
runTestとTurbineで非同期の動作を検証します
「suspend関数とFlowをテストする」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「suspend関数とFlowをテストする」レッスンは無料ですか?
はい。「suspend関数とFlowをテストする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「suspend関数とFlowをテストする」で何を学びますか?
runTestとTurbineで非同期の動作を検証します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「suspend関数とFlowをテストする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- commonTestのkotlin.test
- suspend関数とFlowをテストする
- Ktor Clientとリポジトリをフェイク化する
- AndroidとiOSのターゲットでテストを実行する