# coroutine-testing

Published articles for coroutine-testing.

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

## 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: 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>), [Testing](<https://devfeed.tech/topics/testing.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [test](<https://devfeed.tech/tags/test.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>), [time](<https://devfeed.tech/tags/time.md>)

### AI overview

This tutorial explains virtual time in coroutine tests, focusing on the runCurrent, advanceTimeBy, and advanceUntilIdle APIs. It distinguishes scheduling from execution and demonstrates how StandardTestDispatcher and TestCoroutineScheduler affect test results.

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

## Coroutine Testing - Picking the right Dispatcher

DevFeed: [Coroutine Testing - Picking the right Dispatcher](<https://devfeed.tech/articles/coroutine-testing-picking-the-right-dispatcher-25237.md>)

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

Author: Kaushik Gopal

Published: 2024-08-25T07: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>), [Testing](<https://devfeed.tech/topics/testing.md>), [test](<https://devfeed.tech/topics/test.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [flaky](<https://devfeed.tech/tags/flaky.md>), [scope](<https://devfeed.tech/tags/scope.md>), [strategy](<https://devfeed.tech/tags/strategy.md>), [testing](<https://devfeed.tech/tags/testing.md>)

### AI overview

This tutorial explains how CoroutineContext, CoroutineScope, and Dispatchers affect coroutine tests. It recommends explicitly injecting a CoroutineScope and replacing it with TestScope to control dispatchers, reduce flakiness, and make tests run faster.

### 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) Most of the problems and flakiness around coroutine testing stem from running them on different Dispatchers. This is because the choice of Dispatcher can significantly impact the behavior of coroutines. This was also the most confusing1 part for me starting out -- understanding the implications of using a Scope, Context or Dispatcher. I recommend Roman's article if you want to brush up on the fundamentals. But in a nutshell: think of CoroutineContext as a collection of elements that define the coroutine. It contains a Dispatcher, Job & a CoroutineName. When you launch a coroutine, it inherits the parent's CoroutineContext (and Dispatcher), unless you specify it explicitly. A CoroutineScope on the other hand is just a way to manage and cancel (multiple) coroutines. It also defines a context and lifecycle for the coroutines launched within it (the context could be linked to yet another Dispatcher). Any coroutine when launched, runs within a CoroutineScope. Let's take an example: Notice how the current Dispatcher of the coroutine shifts from StandardTestDispatcher -> UnconfinedTestDispatcher -> Dispatcher.IO in the span of three innocuous lines based on the coroutine builder (runTest) or scope used (TestScope, turbineScope from the 3rd party library, App scope). In my initial post I pointed out this flaky test: flaky test code on github The fix for this is as simple as explicitly injecting a TestScope and making sure the same scope is used throughout. fixed test code on github Explicitly injecting the CoroutineScope and substituting it with the TestScope works really well and is my preferred strategy. This approach allows for more control over the Dispatcher used in tests . For reasons you'll see later, these tests also run instantly (72

## 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: 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>), [Testing](<https://devfeed.tech/topics/testing.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [LineageOS](<https://devfeed.tech/topics/lineageos.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [flaky](<https://devfeed.tech/tags/flaky.md>), [junit](<https://devfeed.tech/tags/junit.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [migration-guide](<https://devfeed.tech/tags/migration-guide.md>), [testing](<https://devfeed.tech/tags/testing.md>), [time](<https://devfeed.tech/tags/time.md>), [tutorials](<https://devfeed.tech/tags/tutorials.md>)

### AI overview

A tutorial series on testing Kotlin coroutines in Android applications. It discusses changes to Kotlin testing APIs, unreliable virtual-time advancement, flaky tests, dispatchers, background scopes, and controlling time.

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

## The conflation problem of testing StateFlows

DevFeed: [The conflation problem of testing StateFlows](<https://devfeed.tech/articles/the-conflation-problem-of-testing-stateflows-27057.md>)

Original publisher: [Read original article](<https://zsmb.co/conflating-stateflows/>)

Author: Márton Braun

Published: 2023-08-15T14:00:00Z

Content type: tutorial

Language: en

Sources: [zsmb.co](<https://devfeed.tech/sources/zsmb-co.md>)

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

Tags: [android](<https://devfeed.tech/tags/android.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlinx](<https://devfeed.tech/tags/kotlinx.md>), [marton-braun](<https://devfeed.tech/tags/marton-braun.md>), [stateflow](<https://devfeed.tech/tags/stateflow.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>), [ui](<https://devfeed.tech/tags/ui.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>), [zsmb](<https://devfeed.tech/tags/zsmb.md>), [zsmb-co](<https://devfeed.tech/tags/zsmb-co.md>), [zsmb13](<https://devfeed.tech/tags/zsmb13.md>), [zsmbco](<https://devfeed.tech/tags/zsmbco.md>)

### AI overview

This article explains how StateFlow conflation affects tests. It compares asserting on the StateFlow state property with collecting emitted values, and discusses testing intermediate values when rapid updates may be skipped by slow collectors.

### Source excerpt

StateFlow behaves as a state holder and a Flow of values at the same time. Due to conflation, a collector of a StateFlow might not receive all values that it holds over time. This article covers what that means for your tests.

## Coroutines on Android

DevFeed: [Coroutines on Android](<https://devfeed.tech/articles/coroutines-on-android-23898.md>)

Original publisher: [Read original article](<https://medium.com/smg-real-estate/coroutines-on-android-d3e3413e6aa7?source=rss----2186e5b9bd8f---4>)

Author: Stevan Milovanovic

Published: 2022-05-20T12:36:23Z

Content type: tutorial

Language: en

Sources: [Homegate Engineering Blog - Medium](<https://devfeed.tech/sources/homegate-engineering-blog-medium.md>)

Topics: [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Android](<https://devfeed.tech/topics/android.md>), [async](<https://devfeed.tech/topics/async.md>), [Concurrent Programming](<https://devfeed.tech/topics/concurrent-programming.md>), [context](<https://devfeed.tech/topics/context.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [android-apps](<https://devfeed.tech/tags/android-apps.md>), [article](<https://devfeed.tech/tags/article.md>), [async](<https://devfeed.tech/tags/async.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [cancellation](<https://devfeed.tech/tags/cancellation.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [programming](<https://devfeed.tech/tags/programming.md>), [thread](<https://devfeed.tech/tags/thread.md>), [threads](<https://devfeed.tech/tags/threads.md>)

### AI overview

This tutorial explains why Kotlin coroutines can be preferable to threads for asynchronous and background processing on Android. It introduces coroutine scopes, contexts, suspending functions, jobs, and dispatchers, and describes using coroutines for networking and background processing.

### Source excerpt

Coroutines on Android In this article I'll try to explain why coroutines are useful and why would you want to use them in your project. After we go through the most important concepts of coroutines, I'll show you how I used coroutines to implement networking and background processing in the example project. First of all, you might ask yourself, why we want to use coroutines over threads? Main problem with threads is that they are resource intensive, meaning it takes a lot of resources to start a thread, stop a thread. Meanwhile, coroutines are lightweight threads, since they use thread pools. Another benefit of coroutines is that they greatly simplify asynchronous code. Callbacks and synchronisation are very easy to use. In fact, they make parallel programming look very much like sequential programming. Coroutines can be paused and resumed at any time, on a number of threads. And lastly, since coroutines are based on a few fairly easy to grasp concepts, their syntax is simple and easy to use. Here are the main concepts we need to explain about coroutines: Scope Coroutine scope, as its name says, defines a scope for new coroutines. Every coroutine builder (like launch and async) is an extension on CoroutineScope and inherits its coroutineContext to automatically propagate all its elements and cancellation. Context Coroutine context represents the context of its scope. Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope. Scope provides a context in which the coroutine runs (state of the coroutine which provides variables, functionality of the coroutine etc.). Suspending functions Suspending functions are functions that can be run in a coroutine. They make callbacks seamless. They can be run in a coroutine (can be suspended) and that is why they can provide functionalities which have to be run in parallel. Job Job is a handle on that coroutine (on the piece of code which runs in the background). A laun

## Hello8Ball: Exploring Coroutine Testing with a Kotlin Sampler App

DevFeed: [Hello8Ball: Exploring Coroutine Testing with a Kotlin Sampler App](<https://devfeed.tech/articles/hello8ball-32055.md>)

Original publisher: [Read original article](<https://www.maiatoday.net/p/hello8ball/>)

Published: 2019-11-23T21:36:11Z

Content type: tutorial

Language: en

Sources: [maiatoday](<https://devfeed.tech/sources/maiatoday.md>)

Topics: [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [test](<https://devfeed.tech/topics/test.md>), [CircleCI](<https://devfeed.tech/topics/circleci.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>), [App](<https://devfeed.tech/topics/app.md>)

Tags: [circleci](<https://devfeed.tech/tags/circleci.md>), [code](<https://devfeed.tech/tags/code.md>), [code-coverage](<https://devfeed.tech/tags/code-coverage.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [detekt](<https://devfeed.tech/tags/detekt.md>), [jacoco](<https://devfeed.tech/tags/jacoco.md>), [kotlinx](<https://devfeed.tech/tags/kotlinx.md>), [test](<https://devfeed.tech/tags/test.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

### AI overview

Hello8Ball is a toy sampler app for exploring coroutine testing in Kotlin. It simulates an 8 Ball and includes examples involving network calls, calculations, password generation, synonym lookup, and prime-number checks. The repository includes tests using kotlinx-coroutine-test, CircleCI test execution, detekt, JaCoCo coverage, and a branch converted to JUnit 5.

### Source excerpt

A sampler app to explore Coroutine testing. This is a toy app that simulates an 8 Ball. It can answer questions, find synonmyms, generate a password or check if a number is prime. It was created to make a situation where it makes sense to use coroutines to e.g. go on the network or make a calculation. Then I added tests for all the pieces using kotlinx-coroutine-test. As a bonus the repo is set up to run the tests on CircleCi. It has detekt setup and jacoco code coverage. There is also a branch where all the tests are converted to junit5. Devfest 2019 video This is the companion repo to the KotlinEveryWhereZA 2019 and DevFestZa 2019 talk. Slides are in the repo. code