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.

Support runBlocking for UI Tests

See original GitHub issue

As 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 to runBlocking which would allow to explicitly by-pass the checkers (only for this call of runBlocking)

Issue Analytics

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

github_iconTop GitHub Comments

14reactions
elizarovcommented, Aug 14, 2018

It is already reverted in develop branch. Will be part of the next build (tentatively 0.24.1).

9reactions
qwwdfsadcommented, Aug 8, 2018

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:

  1. There are cases when blocking in UI is legit
  2. UI check is not well integrated with test frameworks
  3. The workaround with system property for tests is unstable and may lead to production crashes (where this property is not set) and this is the opposite of what we wanted
  4. Flag failsInUI is a debatable boilerplate-like solution. E.g. when somewhen have to write blocking test he may just pass this flag everywhere (because runTest is not well-discoverable or entry point may be hidden in production code)
  5. Enabling this feature only in developer mode is not that useful as it’s opt-in feature (which should be discovered first) and carries much less value
Read more comments on GitHub >

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

github_iconTop Related Medium Post

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