

AAA"How do you approach unit testing and UI testing in a mobile codebase? Explain how you decide what to test at each level, how you keep tests maintainable, and how you handle flaky UI tests."
A strong mobile testing strategy usually follows a pyramid: many fast unit tests, fewer integration tests, and a small number of end-to-end UI tests. This keeps feedback fast while still validating critical user flows.
Unit tests should isolate business logic from networking, storage, time, and platform APIs. Dependency injection makes components replaceable with mocks, fakes, or stubs so tests stay deterministic.
class LoginViewModel:
def __init__(self, auth_service):
self.auth_service = auth_service
UI tests are slower and more fragile because they depend on rendering, async timing, animations, and device state. Stable UI tests rely on deterministic app state, explicit synchronization, and resilient selectors such as accessibility identifiers.
Not every line deserves the same type of test. Core business rules, state transitions, formatting, validation, and error handling belong in unit tests, while a few high-value user journeys belong in UI tests.
Flaky tests reduce trust in the suite and slow teams down. Common fixes include removing arbitrary sleeps, mocking unstable dependencies, resetting state between runs, and running tests in a controlled environment.
wait_until(lambda: screen.is_loaded())
assert screen.error_message == 'Invalid credentials'