Sub topic 14
Measuring Code Coverage
This chapter assumes you know basic Dart async and await, can build simple Flutter widgets, and can run commands with the flutter tool. It also assumes your tests already live in test/.
Code coverage measures which parts of your source code run while tests execute. Flutter can collect coverage from unit tests and widget tests. The result helps you find untested code, but it does not prove that the tested code is correct.
Generate a coverage report
Run this command from the root of your Flutter project:
flutter test --coverageFlutter runs the test suite and writes an LCOV report to:
coverage/lcov.infoLCOV is a text format that records executed and unexecuted source lines. The report is useful to CI systems and tools that display coverage.
A typical workflow is:
- Run all tests with coverage.
- Open or upload
coverage/lcov.info. - Find files with uncovered lines.
- Add tests for important behavior.
- Run the command again.
You can run coverage for a selected test file when investigating one area:
flutter test --coverage test/user_repository_test.dartThe generated report then describes only the tests executed by that command. This can make the percentage look lower or higher than the full-project result, so use the complete test suite for the number reported by CI.
Read coverage correctly
Coverage tools commonly report these measurements:
| Measurement | Meaning | Useful question |
|---|---|---|
| Lines | Source lines executed by tests | Which statements never ran? |
| Functions | Functions called by tests | Which public operations are untested? |
| Files | Files reached by tests | Which parts of the project have no tests? |
Line coverage is usually the easiest measure to understand in a Flutter project. An uncovered line may be an error path, an empty-state branch, or code that your tests never reach.
A high percentage can still hide weak tests. For example, a test might execute a login method but never check whether login succeeds or fails. Test behavior and important outcomes, not just lines.
View an HTML report
The LCOV file is plain text, but an HTML report is easier to browse. If the genhtml command is installed, run:
flutter test --coverage
genhtml coverage/lcov.info -o coverage/htmlOpen coverage/html/index.html in a browser. The report usually shows a project summary, file totals, and highlighted source lines.
Generated files and platform-specific code can distort the result. Keep the project’s coverage rules consistent, and exclude files only when the reason is clear. Do not exclude application logic merely to raise the percentage.
Coverage in continuous integration
A continuous integration system runs the tests on every change. A simple CI step can generate the report and preserve it as a build artifact:
flutter pub get
flutter test --coverage
test -s coverage/lcov.infoThe final command fails if the report is missing or empty. You can add a coverage threshold with a separate tool, but choose the threshold after measuring the existing project. A threshold should prevent accidental drops without blocking useful changes that need a deliberate review.
Exercise
Which command generates Flutter's standard coverage file, and why should you avoid judging test quality from its percentage alone?
Show answer
flutter test --coverage generates coverage/lcov.info. The percentage shows executed code, not whether tests check the right outcomes or failure cases.