How to report code coverage data from Flutter tests?

code coverage

9 June 2022 Stephan Petzl Leave a comment Tech-Help, Test automation

This is a question that gets asked quite often. Initially, this took a lot of setup, see the section below on how this used to work before the release of Flutter 2.5 in September 2021.

At the time of writing this is how you can easily display test coverage:

You can see coverage information for both unit and integration test runs with the newest IntelliJ / AndroidStudio plugin for Flutter. Accessed it by clicking the toolbar button next to the “Debug” button:

And this is how the code coverage is displayed:
The red bars indicate code which hasn’t been covered and and green bars which code has been covered.

Lines 9–13 were tested, but lines 3 and 4 were not.

Alternative ways to get the code coverage:

You can also install lcov and convert the file to HTML pages and then see the result in the browser with sorting option.

1.1. Installation on Ubuntu

sudo apt-get update -qq -y
sudo apt-get install lcov -y

1.2. Installation on Mac

brew install lcov

2. Run tests, generate coverage files and convert to HTML

flutter test --coverage
genhtml coverage/ -o coverage/html

Like this article? there’s more where that came from!