D
TestingMedium30 XP3 min read

How do you measure and improve test coverage in Flutter?

TL;DR: Run flutter test --coverage to generate an lcov.info file. Use lcov or genhtml to convert it to HTML. Target >80% line coverage, but focus on business-critical paths and edge cases rather than chasing 100%.

Full Answer

Coverage tells you which lines were executed during tests — but high coverage doesn't guarantee correct tests.

Coverage Types

  • Line coverage: % of lines executed
  • Branch coverage: % of conditional branches (if/else, switch) covered
  • Function coverage: % of functions called
🎯

Exclude generated files (*.g.dart, *.freezed.dart) and UI-only files from coverage to get meaningful numbers.

Code Examples

bashGenerate and view coverage report
Output
// HTML report shows which lines/branches are covered per file

Common Mistakes

  • Treating 100% coverage as a goal — it leads to shallow tests that just execute code without asserting anything
  • Including generated files in coverage metrics — inflate numbers with trivially covered boilerplate

Interview Tip

💡

Coverage is a lagging indicator. A test that calls a function but doesn't assert its output is 'covered' but useless. Pair coverage with mutation testing for real confidence.

#coverage#lcov#test-coverage#quality