-
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
asynccode is always a challenge in any application. - In Android all the new apps use
suspendfunctions.
- 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
setupthenexecutethen finallyassert. There can be a possibility that we might end upassertingvalues even though the execution phase is not completed which might end upfailing tests. -
Asynccode might lead torace-conditionsof functions being the tests failing sometimes and succeeding sometimes leading toflakytests.
- So since
asyncbehavior is the cause of tests failing, we should make our codeasynctosynchronous. - 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.