Testing without 'runBlocking'
See original GitHub issueHi 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 ☺️
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
runBlockingTest
runBlockingTest ... You can use this to write tests that execute in the presence of calls to delay without causing your test to...
Read more >Unit Testing with Kotlin Coroutines: The Android Way
This test is similar to runBlocking() function, but it will immediately progress past delays and into a launch and async blocks. You can...
Read more >Testing Kotlin Coroutines
We just use runBlocking , and there is nearly no difference between testing how suspending and blocking functions behave.
Read more >Should runBlocking only be used for tests and in main ...
However, I've read in the documentation of runBlocking that it is "to be used in main functions and in tests." This function is...
Read more >Testing Kotlin Coroutines
However, that's not going to compile, since we can't call addTwoInts() from ... runBlocking will block the current thread until the code block...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 callsrunBlocking
on JVM and Native and that usePromise
on Node.js (as most Node.js test libraries handle correctly test cases that return aPromise
it works just fine)Here are the links to my implementations
Hope this helps
This is more or less the thing we do in our tests (+ custom
CoroutineExceptionHandler
andcatch
inpromise
to fail a test on exception)