Introduction
Here is a short guide about how to visualize code coverage of a Flutter app in Visual Studio Code.
Today I want to show you how you can measure your code coverage and see the covered and uncovered lines in your Flutter app. We are going to use Visual Studio Code and two free extensions from the Visual Studio Marketplace:
If you are unfamiliar with the concept of unit testing and how to write tests for a Flutter app please check my other article where I explain the basics of testing.
The test subject
I created a simple class called login_service.dart
. It allows logging in, logging out, and checking the current state (logged in or not). It’s very basic and has flaws (like hard-coded user name and password) but will be enough to demonstrate how coverage works. The class is shown below.
The unit tests
Furthermore, we have some unit tests to cover all possible statements of the LoginService
. You can see it in the file below.
Collect coverage information
Flutter has a built-in command to collect coverage information while executing all tests. So you just need to open a shell of your choice, navigate to the project root folder, and execute the following command flutter test --coverage
.
This will execute all tests in your project and create a file lcov.info
which contains the coverage information. The extensions you installed earlier rely on the file to visualize the information.
Get the Flutter Test Guide for free!
An ebook about unit and widget testing in Flutter applications. Boost your testing skills now with this guide!
Show covered lines in the editor
After the coverage information has been collected open a file that has been tested (in this example the login_service.dart
file). You should see something like this.
A green line means that there is a test executing this code line while a red line means that there is no test executing this code line. This class has a coverage of 100% as all unmarked lines (like field declarations) are not measured by the Flutter tool.
❗
Warning
Be aware that the colors don’t indicate if a test passes or not. They just tell you if there is any test for a line.
If you can’t see any colorings check the status bar and enable the coverage gutters.
Show coverage information per file
In bigger projects, you’d like to have an overview of those files that have sufficient test coverage and those that don’t. To solve this problem you can use the Flutter Coverage extension. It shows the coverage percentage of every dart file in your project. Go to your Test Explorer in VS Code and open the Flutter Coverage drawer. You should see something like this.
The coverage of the login_service.dart
class is 100% as we have concluded before. Especially if your project gets bigger and bigger this view will be quite handy to identify insufficiently tested classes.
Credits
If you like the extensions the developers appreciate a rating or any feedback.
Source code
You can find the source code on GitHub.