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 change1.
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:
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:
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