Kotlinx.coroutines: Testing without 'runBlocking'

Created on 12 Dec 2018  路  5Comments  路  Source: Kotlin/kotlinx.coroutines

Hi guys, coroutines are awesome 馃憤
I just started a new multiplatform library project which is based on coroutines, but I have trouble figuring out how I am supposed to write tests using coroutines.
Since most code is in the 'commonMain' sourceSet I would like to write tests for my suspending functions there. Because Kotlin's @Test annotation does not support suspending tests and 'runBlocking' is not available in the common module of coroutines, I do not see how I could test those suspending functions in the commonTest module. Did I miss something here?

Thanks in advance 鈽猴笍

question

Most helpful comment

runBlocking is not present in the common package because JS platform does not support it.

In my experience, I had to make a common function runTest that calls runBlocking on JVM and Native and that use Promise on Node.js (as most Node.js test libraries handle correctly test cases that return a Promise it works just fine)

Here are the links to my implementations

Hope this helps

All 5 comments

runBlocking is not present in the common package because JS platform does not support it.

In my experience, I had to make a common function runTest that calls runBlocking on JVM and Native and that use Promise on Node.js (as most Node.js test libraries handle correctly test cases that return a Promise it works just fine)

Here are the links to my implementations

Hope this helps

In my experience, I had to make a common function runTest that calls runBlocking on JVM and Native and that use Promise on Node.js

This is more or less the thing we do in our tests (+ custom CoroutineExceptionHandler and catch in promise to fail a test on exception)

Thanks a lot guys! 鈽猴笍

What about Native implementation? :/

I intentionally omit native implementation because it contains library specific functions but it works exactly like JVM as it implements runBlocking

Was this page helpful?
0 / 5 - 0 ratings