# 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: [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Android](<https://devfeed.tech/topics/android.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>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [stateflow](<https://devfeed.tech/tags/stateflow.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

This tutorial explains why coroutine tests can time out when they collect from non-terminating Channels, SharedFlow, StateFlow, or ViewModel jobs. It presents manual Job cancellation and backgroundScope, which cancels child coroutines when the test body completes.

## 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