question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing without 'runBlocking'

See original GitHub issue

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 ☺️

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
SeekDaSkycommented, Dec 12, 2018

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

1reaction
qwwdfsadcommented, Dec 12, 2018

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found