liveBook · Manning

Turbine

As explained in Kotlin in Action, 2E

Turbine is a testing library for inspecting emissions from Kotlin Flow and other asynchronous streams.

Turbine is a third-party Kotlin library for testing flows, especially when they are infinite or require assertions about complex emission sequences and invariants. section 18.5.2

Testing each flow emission

@Test fun doTest() = runTest { myFlow.test { assertEquals(1, awaitItem()) assertEquals(2, awaitItem()) assertEquals(3, awaitItem()) awaitComplete() } } The test extension launches a coroutine and collects the flow internally. The test lambda awaits each item, checks it with ordinary test assertions, and verifies completion explicitly. section 18.5.2

Use Turbine when a simple collection is not enough:

  • Replace collecting a finite flow with toList when you need to inspect emissions incrementally or validate completion. section 18.5.2
  • Use awaitItem, awaitComplete, and awaitError to check values, normal completion, and failures. section 18.5.2
  • Use Turbine’s additional facilities to test multiple flows together or create standalone Turbine objects as test stand-ins. section 18.5.2
Listing 18.15 Testing a flow with Turbine
@Test
fun doTest() = runTest {
   val results = myFlow.test {
       assertEquals(1, awaitItem())
       assertEquals(2, awaitItem())
       assertEquals(3, awaitItem())
       awaitComplete()
   }
}

Shows the compact Turbine test pattern for consuming three emissions and asserting completion. section 18.5.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains the core test extension and await functions, while deferring multiple-flow testing and standalone objects to library documentation.
Examples
Demonstrates testing a flow emitting 1, 2, and 3, then asserting awaitComplete after each awaited item.
What it assumes
Builds on coroutine tests with runTest and ordinary test-framework assertions while focusing specifically on flow behavior.
What you can do after
Apply it via the Turbine library to validate flow emissions, completion, errors, and more complex flow invariants.
How it connects
Connects Kotlin Flow testing with coroutines, runTest, assertions, and flow completion or error handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.