Testing coroutines in a single thread

I have an app that utilizes coroutines with different thread pools, eg. IO and Default.

Is it possible to have a test that uses 1 pool for all dispatchers with 1 thread, basically converting this test to single threaded?

I don’t think there is anything like this out there. You can however wrap all your dispatchers in a small function that checks whether you are running in a test environment and in that case return a different dispatcher. This won’t work with 3rd party libraries using coroutines. Also restricting IO to a single thread might lead to deadlocks.

Also see kotlinx.coroutines.test

I tend to pass CoroutinScopes in as a dependency for coroutine creating classes so tests can use a TestCoroutineScope.

1 Like