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
