# coroutine-testing

Coroutine testing is the practice of testing Kotlin coroutines using kotlinx.coroutines.test utilities, including runTest, test dispatchers, and virtual-time scheduling.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Kotlin Multiplatform Networking with Native Android and iOS ViewModels

DevFeed: [Kotlin Multiplatform Networking with Native Android and iOS ViewModels](<https://devfeed.tech/articles/kotlin-multiplatform-networking-with-native-android-and-ios-viewmodels-134271.md>)

Original publisher: [Read original article](<https://proandroiddev.com/kmp-networking-native-viewmodels-cf6fb357b012?source=rss----c72404660798---4>)

Author: Ravi Rawal

Published: 2026-11-14T14:49:33Z

Content type: tutorial

Language: en

Sources: [ProAndroidDev](<https://devfeed.tech/sources/proandroiddev-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Ktor](<https://devfeed.tech/topics/ktor.md>), [business logic](<https://devfeed.tech/topics/business-logic.md>), [Jetpack](<https://devfeed.tech/topics/jetpack.md>), [REST API](<https://devfeed.tech/topics/rest-api.md>), [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [api](<https://devfeed.tech/tags/api.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [business-logic](<https://devfeed.tech/tags/business-logic.md>), [cancellation](<https://devfeed.tech/tags/cancellation.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [domain](<https://devfeed.tech/tags/domain.md>), [error](<https://devfeed.tech/tags/error.md>), [http](<https://devfeed.tech/tags/http.md>), [http-client](<https://devfeed.tech/tags/http-client.md>), [ios](<https://devfeed.tech/tags/ios.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [kmp](<https://devfeed.tech/tags/kmp.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [native](<https://devfeed.tech/tags/native.md>), [networking](<https://devfeed.tech/tags/networking.md>), [published](<https://devfeed.tech/tags/published.md>), [software-architecture](<https://devfeed.tech/tags/software-architecture.md>)

### AI overview

The article recommends sharing Ktor networking, serialization, repositories, domain models, and meaningful use cases in Kotlin Multiplatform while Android and iOS retain native ViewModels and presentation state. Callers own coroutine or task lifecycles: Android can launch through `viewModelScope`; iOS can bridge Kotlin suspend functions to Swift `async`/`await` with KMP-NativeCoroutines or SKIE and cancel its Swift `Task`. Ktor requests are suspending and need no automatic dispatcher switch when they are main-safe; truly blocking or CPU-heavy work should use an appropriate injected dispatcher. Preserve cancellation, reuse the HTTP client, and keep DTO-to-domain and domain-to-UI mapping at their respective boundaries. The article notes that Swift interop adds generated types and conversions, and shared unit tests do not verify platform HTTP engines or the Kotlin-to-Swift bridge.

### Source excerpt

Share Ktor networking, repositories, and use cases while Android and iOS keep control of their native ViewModels, UI state, threading, and cancellation. Kotlin Multiplatform allows us to share networking and business logic between Android and iOS. When an API call is made from the shared Kotlin module to the native app there are a number of things we need to make a decision on. Who creates the coroutine? Who handles where the work is executed? Will the ViewModel be the same for Android and iOS? How do we get the use cases from the shared module onto each platform? The decisions here are more important than the actual HTTP client. A good KMP architecture will not have all the layers shared between both platforms. We can leave the repositories and use cases in Kotlin, have Android use its Jetpack ViewModels and iOS use SwiftUI with its native Swift ViewModels. Both platforms will call the same business functions but each will have control over their own presentation lifecycle. It's important to make sure the work that needs to be consistent is shared. Let's look at how we can create this architecture using a simple example of fetching articles from a REST API. 1. Decide what belongs in shared code Lets start with two native applications and one shared KMP module. The Android and iOS applications will not need or have identical ViewModels. But they should access to the same business operation. The shared module can own: HTTP client configuration and API requests. DTOs and serialization. Repository implementations and data-to-domain mapping. Use cases that represent application operations. Shared validation, business rules, and error classification. Android can use using Jetpack ViewModel, viewModelScope, and StateFlow. iOS may use SwiftUI, ObservableObject, @Published, and Swift concurrency. This is one of the architecture options described in the Kotlin Multiplatform ViewModel documentation: sharing the repository and data layers while keeping platform-specific ViewMo

## Part 2: Hardening Turbine Tests - Kevin Schildhorn

DevFeed: [Part 2: Hardening Turbine Tests - Kevin Schildhorn](<https://devfeed.tech/articles/part-2-hardening-turbine-tests-kevin-schildhorn-109898.md>)

Original publisher: [Read original article](<https://touchlab.co/flake-free-flow-tests-pt2-turbine>)

Published: 2026-10-30T18:00:00Z

Content type: tutorial

Language: en

Sources: [Touchlab](<https://devfeed.tech/sources/touchlab-enterprise-mobile-innovation-development.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>)

Tags: [ai](<https://devfeed.tech/tags/ai.md>), [ai-skills](<https://devfeed.tech/tags/ai-skills.md>), [android](<https://devfeed.tech/tags/android.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [debounce](<https://devfeed.tech/tags/debounce.md>), [emissions](<https://devfeed.tech/tags/emissions.md>), [event-streams](<https://devfeed.tech/tags/event-streams.md>), [flow](<https://devfeed.tech/tags/flow.md>), [has](<https://devfeed.tech/tags/has.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [testing](<https://devfeed.tech/tags/testing.md>), [turbine](<https://devfeed.tech/tags/turbine.md>)

### AI overview

To reduce flaky Turbine tests, match assertions and scheduler control to the kind of flow and the behavior under test. For StateFlow and other state holders, prefer checking the current value because rapid updates, equality, or upstream operators can hide intermediate emissions; assert a sequence only when it is part of the contract, and make its timing deterministic. Start collectors before triggering hot-flow events, use runCurrent() to process work queued at the current virtual time, advanceUntilIdle() for end states, and advanceTimeBy() plus runCurrent() for precise time boundaries. Add sync points before expectNoEvents() or expectMostRecentItem(), which otherwise can inspect a buffer before asynchronous work arrives. The excerpt also cautions that shareIn with replay = 0 and stateIn on StandardTestDispatcher may miss hot-source emissions before collection starts; starting collection first or using an Unconfined dispatcher can address that gap. End tests by consuming completion or cancelling and ignoring irrelevant remaining events, and configure Turbine's wall-clock timeout for real-dispatcher work.

### Source excerpt

Testing Flows in your app can be confusing, it's important to know how to avoid flakiness in your Turbine testing.

## Kotlin Flow: Bugs That Look Correct at First

DevFeed: [Kotlin Flow: Bugs That Look Correct at First](<https://devfeed.tech/articles/kotlin-flow-bugs-that-look-correct-at-first-105704.md>)

Original publisher: [Read original article](<https://kt.academy/article/kaustubh_flow_bugs_that_look_correct_at_first>)

Published: 2026-10-26T00:00:00Z

Content type: article

Language: en

Sources: [Kt. Academy](<https://devfeed.tech/sources/kt-academy.md>)

Topics: [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [bugfixes](<https://devfeed.tech/topics/bugfixes.md>), [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [Exception handling](<https://devfeed.tech/topics/exception-handling.md>), [Exception](<https://devfeed.tech/topics/exception.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [api](<https://devfeed.tech/tags/api.md>), [apis](<https://devfeed.tech/tags/apis.md>), [buffer](<https://devfeed.tech/tags/buffer.md>), [bugs](<https://devfeed.tech/tags/bugs.md>), [cancellation](<https://devfeed.tech/tags/cancellation.md>), [capacity](<https://devfeed.tech/tags/capacity.md>), [collector](<https://devfeed.tech/tags/collector.md>), [collectors](<https://devfeed.tech/tags/collectors.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [database](<https://devfeed.tech/tags/database.md>), [error-handling](<https://devfeed.tech/tags/error-handling.md>), [exception](<https://devfeed.tech/tags/exception.md>), [exceptions](<https://devfeed.tech/tags/exceptions.md>), [execution](<https://devfeed.tech/tags/execution.md>), [flow](<https://devfeed.tech/tags/flow.md>), [workshop-learning-programming](<https://devfeed.tech/tags/workshop-learning-programming.md>)

### AI overview

The excerpt explains four Kotlin Flow pitfalls and how to address them. Collecting a cold flow separately for multiple consumers reruns its upstream work; use `shareIn()` when those consumers should share one collection, choosing a suitable scope and `SharingStarted` policy. `catch` handles failures upstream of its position, so move operations whose exceptions it should handle before `catch`; cancellation should generally propagate. `conflate()` may replace pending values with newer ones when a collector is slow, so use it only when intermediate values can be skipped; buffering can decouple producer and consumer, but a finite buffer may fill. Finally, `stateIn()` with `WhileSubscribed()` will not start upstream until there is a subscriber, so a test that only calls `runCurrent()` can still see the initial value. Use `Eagerly` for immediate collection or start a collector when testing subscription-driven behavior.

### Source excerpt

Four well-known Kotlin Flow bugs that look correct at first, why they happen, and how to fix them

## Hardening Coroutine Tests - Kevin Schildhorn

DevFeed: [Hardening Coroutine Tests - Kevin Schildhorn](<https://devfeed.tech/articles/hardening-coroutine-tests-kevin-schildhorn-98452.md>)

Original publisher: [Read original article](<https://touchlab.co/flake-free-flow-tests-pt1-coroutines>)

Published: 2026-10-23T18:00:00Z

Content type: article

Language: en

Sources: [Touchlab](<https://devfeed.tech/sources/touchlab-enterprise-mobile-innovation-development.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [scheduling](<https://devfeed.tech/topics/scheduling.md>)

Tags: [background-work](<https://devfeed.tech/tags/background-work.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [cache](<https://devfeed.tech/tags/cache.md>), [channel](<https://devfeed.tech/tags/channel.md>), [ci](<https://devfeed.tech/tags/ci.md>), [cleanup](<https://devfeed.tech/tags/cleanup.md>), [clock](<https://devfeed.tech/tags/clock.md>), [clocks](<https://devfeed.tech/tags/clocks.md>), [collector](<https://devfeed.tech/tags/collector.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [coroutinescope](<https://devfeed.tech/tags/coroutinescope.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [delay](<https://devfeed.tech/tags/delay.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [emissions](<https://devfeed.tech/tags/emissions.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [scheduler](<https://devfeed.tech/tags/scheduler.md>), [tests](<https://devfeed.tech/tags/tests.md>), [turbine](<https://devfeed.tech/tags/turbine.md>)

### AI overview

Use StandardTestDispatcher with the runTest testScheduler, and inject dispatchers, scopes, and clocks so test work uses controlled virtual time. Drive scheduled work with runCurrent, advanceTimeBy, or advanceUntilIdle; inject TimeSource when code reads elapsed time or timestamps. Use backgroundScope for endless observers and settle it with runCurrent or advanceTimeBy; use a cancellable SupervisorJob scope for finite fire-and-forget work. Avoid parallel tests that set global Dispatchers.Main, and reset it after use. For necessary real-thread tests, keep most work virtual, synchronize on signals such as join or awaitItem rather than sleeps, use generous timeouts, cancel created scopes, and assert final values or invariant patterns instead of exact emission sequences. The supplied article excerpt reports substantial ordering variation and stale results in its repeated experiments, and warns that detached failures or shared state can cause false passes and cross-test flakiness.

### Source excerpt

Why coroutine tests flake, and the dispatcher and scheduler habits that stop it.

## Coroutine Testing - Controlling time

DevFeed: [Coroutine Testing - Controlling time](<https://devfeed.tech/articles/coroutine-testing-controlling-time-25238.md>)

Original publisher: [Read original article](<https://kau.sh/blog/coroutine-testing-time/>)

Author: Kaushik Gopal

Published: 2024-09-04T07:00:00Z

Content type: article

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>)

Tags: [apis](<https://devfeed.tech/tags/apis.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [scheduled](<https://devfeed.tech/tags/scheduled.md>), [testing](<https://devfeed.tech/tags/testing.md>), [time](<https://devfeed.tech/tags/time.md>), [virtual](<https://devfeed.tech/tags/virtual.md>)

### AI overview

The excerpt explains that coroutine test "time" is the TestCoroutineScheduler's ordering of scheduled work. Its three controls differ: runCurrent executes work scheduled at the current virtual time; advanceTimeBy moves the clock by a specified duration, while tasks reached at the boundary may still need execution; advanceUntilIdle keeps advancing until no tasks remain. With StandardTestDispatcher, the examples show that advancing by a delay can schedule a coroutine's next emission without running it yet, while advancing past the scheduled time can trigger execution automatically. The excerpt cautions that without explicitly using StandardTestDispatcher, these controls may work differently than expected.

### Source excerpt

series This is part of a series of posts on Coroutine Testing: Picking the right Dispatcher Never ending tests & backgroundscope Controlling time <- Helpful @Junit TestRule extension (coming soon) Full USF example for Android (coming soon) My journey with coroutine testing started with this "simple" requirement -- to control virtual time in concurrent logic. From my previous post: ... If you don't use a StandardTestDispatcher explicitly, then operators like runCurrent, advanceTimeBy etc. have no meaning. I have a confession to make. I was coy saying "have no meaning". I didn't say "won't work" because in reality those apis will "work" just not in the way you'd expect. Let's dive deeper. The 3 apis # There are 3 important apis in coroutine tests to play with time: runCurrent - execute tasks scheduled at the current moment of virtual time advanceTimeBy - advance virtual time by a number of milliseconds and then execute tasks scheduled in the meantime. advanceUntilIdle - similar to advanceTimeBy but instead of advancing by a specific number of milliseconds, it keeps advancing until no more scheduled tasks are found. In the context of coroutine testing, when you think of "time", it refers simply to the order that the TestCoroutineScheduler decides to execute your scheduled coroutines. That's why the definitions above stress on words "execute" vs "scheduled". In reality, these apis belong to the class TestCoroutineScheduler (not StandardTestDispatcher as the coy comment might have indicated). Let's attempt to understand this all, with some code. StandardTestDispatcher & the 3 apis # This animation should illustrate how the different apis are designed to work.1 Your browser doesn't support HTML5 video. Download the video instead. StandardTestDispatcher is good about respecting the 3 apis. A basic test demonstrating the use of runCurrent that shouldn't be surprising: @Test fun test() = runTest(StandardTestDispatcher()) { var result = "X" launch { result = "A" delay(1.seconds)

## Coroutine Testing - Never ending tests & backgroundScope

DevFeed: [Coroutine Testing - Never ending tests & backgroundScope](<https://devfeed.tech/articles/coroutine-testing-never-ending-tests-backgroundscope-25236.md>)

Original publisher: [Read original article](<https://kau.sh/blog/coroutine-testing-backgroundscope/>)

Author: Kaushik Gopal

Published: 2024-08-30T07:00:45Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [channel](<https://devfeed.tech/tags/channel.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [flow](<https://devfeed.tech/tags/flow.md>), [job](<https://devfeed.tech/tags/job.md>), [jobs](<https://devfeed.tech/tags/jobs.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [test](<https://devfeed.tech/tags/test.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>), [unit-testing](<https://devfeed.tech/tags/unit-testing.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

### AI overview

Coroutine tests can time out even after assertions pass when a test waits for a child job whose source never completes. Channels and hot flows such as SharedFlow and StateFlow do not terminate on their own; ViewModel jobs launched in viewModelScope are normally canceled by the lifecycle, but unit tests may not have access to that scope. The excerpt gives two remedies: cancel the job directly when it is accessible, or launch the coroutine in the test-only backgroundScope, whose child coroutines are explicitly canceled when the test body ends. The supplied text cuts off during its explanation of how backgroundScope works.

### Source excerpt

series This is part of a series of posts on Coroutine Testing: Picking the right Dispatcher Never ending tests & backgroundscope <- Controlling time Helpful @Junit TestRule extension (coming soon) Full USF example for Android (coming soon) If you've spent some time testing Coroutines this exception should look familiar: After waiting for 1m, the test coroutine is not completing, there were active child jobs This tends to happen when you have a coroutine job in your test, that fails to complete on its own. Let's take a simple example. src on github We use a Channel here which is the proverbial event bus for Coroutines. source: kotlinlang.org Channels don't terminate on their own. So when you run a simple test checking the emission, while the items might get collected correctly per the assert statement in the test, the test itself fails like so: test on github The test here is waiting for that coroutine job to complete, which in turn requires the Channel to, but that never happens and the test times out. Where else would I run into this problem? # Channels aren't the only case you'll run into this problem. For example if you use a "hot" Flow like SharedFlow or StateFlow, they don't terminate on their own, so the onus is on you to complete or cancel their Job in tests. Android developers can frequently run into this problem too if you use ViewModels and have an internal StateFlow providing your "view level data" (what i personally like to call "view state"). You typically use the viewModelScope to launch internal coroutine jobs in a ViewModel. The OS then calls the lifecycle method onClear when the Activity or Fragment no longer needs the ViewModel where all jobs started in the viewModelScope are canceled. But when unit testing these ViewModels, you don't have access to the viewModelScope and shouldn't need it anyway. Solving this problem # There's two ways to solve this problem: 1. Manually cancel the Job ## If you have access to the Job that spawns the never-ending co

## Coroutine Testing

DevFeed: [Coroutine Testing](<https://devfeed.tech/articles/coroutine-testing-25239.md>)

Original publisher: [Read original article](<https://kau.sh/blog/coroutine-testing/>)

Author: Kaushik Gopal

Published: 2024-08-25T07:00:45Z

Content type: article

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Android Testing](<https://devfeed.tech/topics/android-testing.md>)

Tags: [concurrency](<https://devfeed.tech/tags/concurrency.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [flaky-tests](<https://devfeed.tech/tags/flaky-tests.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [migration-guide](<https://devfeed.tech/tags/migration-guide.md>), [testing](<https://devfeed.tech/tags/testing.md>)

### AI overview

The excerpt describes coroutine testing challenges after Kotlin testing API changes in version 1.6.0: older tutorials may be obsolete, time-control APIs seemed unreliable to the author, and tests that pass alone can fail in a suite or take several seconds. The series covers choosing a dispatcher, background scope and never-ending tests, controlling time, and a JUnit TestRule extension; it points readers to Untangling Coroutine Testing, JetBrains and Android documentation, and the 1.6.0 migration guide. The excerpt does not explain solutions to the described failures.

### Source excerpt

When the #androiddevs transitioned from Rx to coroutines the topic of testing didn't get as much attention in this new world of concurrency. It didn't help that there was a seismic change in Kotlin's testing apis with 1.6.0. A whole bunch of online resources and tutorials are now defunct courtesy this change.1 My journey in the matter started because I simply couldn't understand why test apis like advanceTimeBy wouldn't work reliably for me. The name made sense... but my time wasn't being advanced in any meaningful way. Then there's the issue of flaky tests. Here's an example: flaky test code on github Run each test individually and it will pass; run them together as one test suite and test2 alone will fail. test1 passes but it takes a full 3s to run the test. If I have 300 of these in my app, are my tests going to take 15 minutes to run? I needed to understand many core concepts in order to confidently explain all the above phenomena. I'd like to share my learnings from going down the rabbit hole, in this series of posts: series This is part of a series of posts on Coroutine Testing: <- Picking the right Dispatcher Never ending tests & backgroundscope Controlling time Helpful @Junit TestRule extension (coming soon) Full USF example for Android (coming soon) If you're looking for the most current and useful resources on coroutine testing today: Untangling Coroutine Testing - Marton Braun jetbrains official docs developer.android.com docs 1.6.0 Coroutines test migration guide ↩︎

## Test Coroutine Scheduler

DevFeed: [Test Coroutine Scheduler](<https://devfeed.tech/articles/test-coroutine-scheduler-22665.md>)

Original publisher: [Read original article](<https://www.valueof.io/blog/test-coroutine-scheduler-dispatcher-main-rule>)

Author: James Shvarts

Published: 2022-06-24T00:55:12Z

Content type: tutorial

Language: en

Sources: [Android Blog - Mobile Dev Notes](<https://devfeed.tech/sources/android-blog-mobile-dev-notes.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>)

Tags: [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [scheduler](<https://devfeed.tech/tags/scheduler.md>), [test](<https://devfeed.tech/tags/test.md>)

### AI overview

For JVM unit tests that launch coroutines through a scope bound to Main, the excerpt says to add the latest `kotlinx-coroutines-test` dependency and install a test dispatcher as Main. It recommends `UnconfinedTestDispatcher` for Main because its eager behavior matches production more closely. `StandardTestDispatcher` queues coroutines for manual advancement and is created by default with `runTest`; `UnconfinedTestDispatcher` starts coroutines eagerly and is suggested selectively for migrating older tests, Main, or collecting values. The excerpt cautions against deprecated `TestCoroutineDispatcher`.

### Source excerpt

Using TestCoroutineScheduler, a scheduler for coroutines used in tests, that provides the delay-skipping behavior.