The Android coroutine testing <docs> say that if y...
# coroutines
c
The Android coroutine testing docs say that if you create your own
TestScope
(e.g. to inject into the class under test), then you must call
runTest
on that scope, because there can only be one
TestScope
instance in a test. But it doesn't say why? Doing this causes my
runTest
block to timeout and fail the test, because the class I injected the
TestScope
into uses it to
shareIn
, which never completes. So my question is what are the repercussions if I disregard this rule and instead call
runTest(testScope.testScheduler)
(which passes), rather than
testScope.runTest
as the docs suggest (which fails). Or, if I should really be using the recommended way, how do I get it to not time out for my use case?
d
c
Thank you! So if I call
testScope.runTest
for my test case, but my class uses
testScope.backgroundScope
instead of the base
testScope
, that doesn't violate the guidelines of "only one
TestScope
for the whole test"? Also, can you point me to what the downsides are if one violates this rule?
d
backgroundScope
is not a
TestScope
, so the rule is not violated. For now, the downsides are, you will lose the exceptions that happen in coroutines in a
TestScope
on which
runTest
is not called. Further down the road, new downsides may appear. We simply don't support the case of more than one
TestScope
.
181 Views