Support runBlocking for UI Tests
See original GitHub issueAs mentionned by @PaulWoitaschek in #458, there is a problem with the fact that runBlocking
fails by default in a UI thread (see #227).
The problem is that if one wants to perform UI tests, then (depending on tool used) the test methods may be called on the UI thread. (this is apparently the case for android, and it is the case for TestFx with Junit5)
Then there is actually no good solution for theses tests. Yes, one could run the tests using the system property introduced in #458, and the tests will pass. But this is not a good solution, because the tests will pass even if there is a runBlocking
in the actual production code, and we’d end-up with passing tests, but failing application.
So here is two proposed solutions:
- Expose a
runTest
method which would not fail, even if in UI thread (proposed by @PaulWoitaschek). - Add an
failsInUI
(or better named) argument torunBlocking
which would allow to explicitly by-pass the checkers (only for this call of runBlocking)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:14 (8 by maintainers)
Top Results From Across the Web
Should runBlocking only be used for tests and in main function?
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 >runBlockingTest - Kotlin
runBlockingTest ... Executes a testBody inside an immediate execution dispatcher. This method is deprecated in favor of runTest. Please see the migration guide ......
Read more >Unit Testing with Kotlin Coroutines: The Android Way - Medium
Testing suspend functions with runBlocking() and runBlockingTest(). A suspending function is a function that can be paused and resumed at a ...
Read more >runBlocking in Kotlin Coroutines with Example - GeeksforGeeks
It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and...
Read more >Testing Kotlin Coroutines - Kt. Academy
We just use runBlocking , and there is nearly no difference between testing how suspending and blocking functions behave. Testing time dependencies. The ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
It is already reverted in
develop
branch. Will be part of the next build (tentatively0.24.1
).Such features are usually implemented as default safety mechanism against accidental misusages, but when they start to make programming experience worse or require explicit opt-in (especially on every call-site) it’s a good marker that feature is harmful.
I think we should revert this change:
failsInUI
is a debatable boilerplate-like solution. E.g. when somewhen have to write blocking test he may just pass this flag everywhere (becauserunTest
is not well-discoverable or entry point may be hidden in production code)