The app had 105,000 lines of Dart spread across 757 files. Firebase Remote Config, Firestore, FFI calls into Rust for GPS processing, SharedPreferences for half the state management. Zero tests.
Not "low coverage." Zero. No test directory, no test runner config, no mocks. Seven months of production use on 30 devices, and the first flutter test invocation in the project's history was mine.
I wrote a preprint documenting how we got from that zero to 2,583 tests across 292 files. The paper and all companion data are open access on Zenodo: DOI: 10.5281/zenodo.24260880.
This post covers the key findings. The paper has the full methodology.
The Problem Nobody Warns You About
Every Flutter testing tutorial starts with a counter app. Tap a button, verify the number. That is not the hard part.
The hard part is a LocationService that wraps Geolocator, which wraps a platform channel, which talks to a GPS chip that returns different accuracy values depending on whether you're indoors, moving, or on a Huawei device where PowerGenie has throttled location updates to once every 15 minutes. You can't mock what you can't define the boundary for.
We had 43 platform-dependent classes. Each one touched at least one of: Firebase, native GPS via FFI, SharedPreferences, or package_info_plus. The first month was spent not writing tests, but drawing mock boundaries.
What Worked
Coverage-exclusion policy. We excluded generated files (*.g.dart, *.freezed.dart) and pure platform-channel wrappers from coverage metrics. Not from testing, from metrics. The distinction matters: we still tested the wrappers, but a wrapper that delegates to MethodChannel('gps') and returns the raw result has exactly one interesting failure mode (the channel throws), and counting its lines toward coverage targets distorts where effort should go.
Mock-boundary pattern. Every platform dependency got a thin abstract interface. The real implementation lives in one file; the mock in another. No when().thenReturn() chains longer than three lines. If the mock setup is more complex than the code it replaces, the boundary is in the wrong place.
17 custom lint rules via kx_lints. We published these as an open-source package on pub.dev. Three of the rules exist solely because test files kept importing production Firebase instances. One rule (no_direct_firebase_access) caught 23 violations in its first week. The rules are the cheapest part of the whole system: write once, enforce forever.
Six Cases Where Tests Caught Production Bugs
The paper documents six specific instances where the test suite caught bugs that would have reached production. I'll describe two here.
Case 1: GPS despiking. Our Rust FFI module processes raw GPS coordinates and filters spikes (sudden jumps that occur when the device switches between GPS and network location). A test for the despike threshold caught a regression where a refactor changed the spike detection from 200m to 2000m. That's the difference between "filters the occasional multipath reflection" and "filters nothing." The test failed, the commit was reverted, and 30 field workers never saw jumps in their route history.
Case 2: SharedPreferences race. Two cubits wrote to the same SharedPreferences key on shift-end. The tests caught this because they ran both cubits in sequence and asserted on the final state. In production, the race would have silently dropped punch-out timestamps for about 8% of shifts (we measured the frequency in staging after the tests flagged it).
The Numbers
- 2,583 tests across 292 test files
- 43 platform-dependent classes with clean mock boundaries
- 17 custom lint rules (13 org-wide via
kx_lints, 4 project-specific) - 6 documented cases where tests caught real production bugs before deployment
The companion repository is at github.com/GhagSagar23/zero-to-tested. The lint rules are at kx_lints on pub.dev.
What I'd Do Differently
I wouldn't start with unit tests. I'd start with the lint rules. Three weeks of writing tests before the no_direct_firebase_access rule existed meant three weeks of finding violations by hand in PR review. The lint rules should have come first; they're the cheapest safety net and they make every subsequent test easier to write correctly.
I also wouldn't chase 100% line coverage. We settled at a policy that excludes generated code and pure wrappers, and that policy is documented and enforced by CI. The number on the badge matters less than whether the tests actually catch the bugs that matter to your users.
The full paper is free to read: doi.org/10.5281/zenodo.24260880
If you've done something similar in a Flutter codebase (or any mobile codebase with heavy platform integration), I'd like to hear how you handled the mock-boundary problem. Drop a comment or find me on GitHub.