-
Notifications
You must be signed in to change notification settings - Fork 0
Async Testing β Why testing async code blocks is hard
Devrath edited this page Oct 25, 2023
·
2 revisions
- Testing
async
code is always a challenge in any application. - In Android all the new apps use
suspend
functions.
- It is very hard to predict the order in which async code blocks are executed.
- Since the tests are executed synchronously, and all the async code runs parallel, this might cause the test to break.
- So as we know tests are set linearly like
setup
thenexecute
then finallyassert
. There can be a possibility that we might end upasserting
values even though the execution phase is not completed which might end upfailing tests
. -
Async
code might lead torace-conditions
of functions being the tests failing sometimes and succeeding sometimes leading toflaky
tests.
- So since
async
behavior is the cause of tests failing, we should make our codeasync
tosynchronous
. - This will enable tests to run on a single thread so we can assert values as we know what happens next.
- We can just replace the normal dispatcher of the coroutines with a test dispatcher so that we can make it synchronous.