How do you measure and improve test coverage in Flutter?
TL;DR: Run 'flutter test --coverage' to generate coverage/lcov.info. Use genhtml to view an HTML report locally or upload to Codecov/Coveralls. Aim for 80%+ coverage on business logic. Coverage is a quality indicator, not a goal — 80% meaningful coverage beats 100% trivial tests.
Full Answer
Coverage measures which lines of code are executed by tests. Flutter generates LCOV format, the industry standard for coverage reports.
What to measure
- ▸Business logic (use cases, repositories, services): aim for 90%+
- ▸BLoC/Cubit state logic: aim for 85%+
- ▸Widget code: 60-70% is often sufficient (visual aspects need golden tests)
- ▸Generated code (freezed, json_serializable): exclude from coverage
Exclude generated files from coverage: flutter test --coverage then filter lcov.info to remove *.g.dart, *.freezed.dart before calculating percentage.
Code Examples
// coverage/lcov.info: line-by-line execution data // HTML report: visual coverage with red/green line highlighting // Codecov: tracks coverage over time, comments on PRs // Filtered: excludes generated code from percentage
Common Mistakes
- ✗Including generated code (*.g.dart) in coverage — inflates uncovered code percentage unfairly
- ✗Aiming for 100% coverage — incentivizes writing trivial tests to hit lines, not meaningful tests
Interview Tip
Coverage percentage is a proxy metric. Show you know: meaningful coverage of business logic matters more than blanket 100%. Also show you know how to exclude generated files — many teams miss this.