android / android/nowinandroid
[FR]: Enhance flow testing methodology using backgroundScope
- Dominant language
- Kotlin
- Stars
- 21.8k
- Forks
- 4.6k
- Avg merge
- 19h 20m
- Merged PRs (30d)
- 2
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the problem
Currently, our flow testing methodology in the NowInAndroid project requires manually creating and canceling job objects to collect flows during tests. This approach can be error-prone and doesn't align with the latest best practices recommended in the Android developer documentation. (example in viewModel test )
### Describe the solution
I propose updating our flow testing methodology to utilize backgroundScope for collecting flows. This solution offers several benefits:
1. Eliminates the need to manually create and cancel job objects.
2. Provides automatic cancellation of the coroutine before the end of the test.
3. Aligns with the latest best practices as documented by Android developers.
Implementation details:
• Replace current job creation and cancellation with backgroundScope.
• Update relevant test cases to use this new methodology.
• Ensure all tests pass with the new implementation.
References:
1. https://developer.android.com/kotlin/flow/test
2. https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-scope/background-scope.html
**i have already created a pull request implementing this change: https://github.com/android/nowinandroid/pull/1585**
### Additional context
This change will simplify our test code, reduce the risk of forgetting to cancel job objects, and improve our alignment with current Android development best practices. It will make our tests more robust and easier to maintain in the long run.
### Code of Conduct
- [X] I agree to follow this project's Code of Conduct
Contributor guide
Research direction
Start by reviewing the existing flow collection pattern in the ViewModel test and compare it with the Android developer documentation on backgroundScope. Identify the other relevant flow tests that manually create and cancel jobs, then run the test suite to confirm the updated methodology passes without manual cancellation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile, testing
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100