Top 3 ways to view code coverage in Flutter

Endre Vekony
3 min readNov 24, 2022

--

Code coverage is a must have when you are writing a Flutter app.

Code coverage is a software testing metric that determines the number of lines of code that were successfully verified as part of a testing procedure, helping you analyze how comprehensively your software is verified.

Developing enterprise-grade software products is the ultimate objective of any software company. In order to achieve this goal however, companies must ensure that their software products meet all important quality criteria such correctness, reliability, effectiveness and security. These are only achievable through thorough testing.

The first command, to generate code coverage is:

flutter test --coverage

First Method — Inside IDE with Extensions

The first and simpliest method is to check the coverage in your preferred IDE, but I think we can do better than this.

For Android Studio developers, install Flutter Enhancement Suite.

For VSCode developers, install Flutter Coverage.

These extensions will show exactly which line is covered, partially covered, or not covered at all.

The biggest con is, these run only in your preferred IDE, and you cannot export them, or save them anywhere. So I think the other options are way better.

Second Method — LCOV

LCOV is an extension of GCOV, a GNU tool that provides information about which parts of a program are actually covered during execution of a particular test case.

So let’s install LCOV

on Linux devices:

sudo apt-get install lcov

On OSX using HomeBrew:

brew install lcov

On Windows using Chocolatey:

choco install lcov

After successful installation of lcov we should generate our coverage in HTML view, with the next commands:

genhtml coverage/lcov.info

Then head to coverage/html/index.html, and open it in your browser, and there you have your interactive code coverage.

Third Method — CSV

In my opinion this is the best way to view coverage, because it is easy, and you can give the .csv file to anyone, and they will understand how much of your code is covered.

first we want to import https://pub.dev/packages/test_cov_console to our pubspec.yaml, or simply add it with:

dart pub global activate test_cov_console

Then run:

flutter pub run test_cov_console --file=coverage/lcov.info --multi -c

Then open coverage/test_cov_console.csv, with your preferred CSV Editor.

As I am using OSX, I open it in Numbers app, and here is the result:

I hope my methods help you find your favorite ways to read code coverage and consider using them in your best projects.

--

--

Endre Vekony

Senior Flutter developer at day, free Tech support for my mom, and the entire family at night.