https://kotlinlang.org logo
#coroutines
Title
# coroutines
v

vitrox1

01/02/2018, 3:37 PM
Hello guys anyone here knows if there’s a way to test coroutines with espresso I mean is not detecting the async process as usually espresso does, so is going forward into the testing code without await ?
l

louiscad

01/02/2018, 3:38 PM
@vitrox1 I'd use
Copy code
runBlocking {
    ...//your test with async code that you await
}
from kotlinx.coroutines
👍 1
v

vitrox1

01/02/2018, 3:42 PM
thanks i’ll try it then!
s

streetsofboston

01/02/2018, 4:05 PM
I used this to not use
runBlocking
and have my tests finish quickly
l

louiscad

01/02/2018, 4:08 PM
@streetsofboston Not using
runBlocking
or similar means any exception in your tests that happens async or after any suspension point don't get caught silently! 🚨
s

streetsofboston

01/02/2018, 4:10 PM
That’s true. But sometimes you can’t have your test-suite take seconds go run, especially when using
delay
and such. I wonder if it is possible to catch exceptions when not using runBlocking when creating your own CoroutineContext.
l

louiscad

01/02/2018, 4:11 PM
@streetsofboston The long tests are not meant to be run on each build or each modification. Also, IIRC, there's test duration hint annotations for Android (which seems to be what you're using)
Also, if you test your code, it's that you have doubts 🤔 🧌 More seriously, it's important to make sure your tests are right and don't pass silently ignoring unexpected failures. Flaky tests are worst than no tests
s

streetsofboston

01/02/2018, 4:14 PM
Yes, you’re right that you can annotate your tests, but for some things you’d like your tests to run as fast as possible, much like when your code under test encounters Rx Java. I’ll look if I can modify my
TestCoroutineContext
to catch any thrown exception and have it report it.
l

louiscad

01/02/2018, 6:12 PM
@streetsofboston FYI,
runBlocking { … }
has seen performance improvements in a very recent update (0.20 IIRC)
3 Views