Module 13 Β· Coroutines & Flow in Anger β Lesson 4 of 4 Β· ~9 min
Testing with runTest & Turbine
Virtual time: runTest
Coroutine code is full of delay(30_000) and 5-second WhileSubscribed windows β real ones would make every test glacial. runTest (from kotlinx-coroutines-test) runs suspending test bodies on a virtual clock: a delay inside code launched from runTest advances that virtual clock instantly instead of actually pausing the test thread, so a test exercising a 30-second polling loop finishes in milliseconds.
runTest { ... } replaces the runBlocking { ... } you might reach for by instinct β same job (give a suspend-capable body to run), but with the virtual-time scheduler wired in, and it also surfaces uncaught exceptions from any coroutine launched inside it as a test failure, instead of them silently vanishing.