Test coverage in Dart & Flutter

Unit tests improve productivity dramatically. With enough coverage, you can make changes quickly while ensuring you have not broken any significant piece.

How it works

You run your unit tests and a tool measures which lines have been tested. The information is stored in an lcov*.info file. Later an IDE or other tool can use it to show you reports or highlight which lines have not been covered.

Generate the lcov file

Flutter

This command runs the tests and stores the coverage info to coverage/lcov.info.

flutter test --coverage

On https://github.com/flutter/flutter/wiki/Test-coverage-for-package:flutter, they mention merging with a base lcov file, but that's only because they pregenerate that base file for their project.

Dart

See the docs. Install coverage:

pub global activate coverage

Run the tests:

dart --pause-isolates-on-exit --enable-vm-service=NNNN test/*

Collect data into coverage.json:

collect_coverage -o coverage/coverage.json --uri=[theuri output the previous command] 

Convert to lcov:

format_coverage -l -i coverage/coverage.json -o coverage/lcov.info --packages=.packages --report-on=lib

report-on filters the coverage to only my own code, and not any its dependencies.

I tried to pipe collect_coverage into format_coverage to run it in one command, but format_coverage does not support it. It fails because it requires an input file or directory.

Viewing the data

As a global report

Install lcov. On my Mac I ran: brew install lcov. I found the command in this post. Once installed, run:

genhtml coverage/lcov.info -o coverage

-o specifies the output directory. If you don't specify it, it will pollute your lib folder with a bunch of html files.

Inline in Visual Studio

In Visual Studio Code, open the Extensions tab and install Coverage Gutters. Once you've reloaded, you have two options:

  • See it once in a file. If you close and reopen the file, the coloring will be gone. To do so, run the command "Coverage Gutters: Display Coverage" from the Command Palette in the file that you wish to analyze. Surprisingly it takes a few even though you are viewing only one file. And my CPU ran hot even in a small project.
  • See it in all files, always: open the lcov file, and click Watch. The extension will take a few minutes, and eventually display code coverage in your dart code by highlighting lines in red or green.

And it does not matter if your coverage/ folder is at the root of your workspace, or if your workspace contains several projects.

However, it is still hit and miss. I have not completely figured this out yet.