How to get your "actual" test coverage of your Flutter applications?

Muhammed Salih Guler
Flutter Community
Published in
4 min readMar 24, 2021

--

https://unsplash.com/photos/dQf7RZhMOJU

Writing tests is one of the most important principles in software development. They help you to prevent regressions, possible bugs and they save a lot of money.

Flutter has a lot of options for you to write tests.. Those are Unit tests, Widget Tests, and Integration tests. With all of these options, when you write a Flutter application, as a good citizen, you would be writing tests with it.

Running the Tests

Flutter has a build-in command to run tests and display the test coverage. It looks as follows.

This command will run all tests and create a coverage file for you under coverage/lcov.info. Be aware that the coverage reports might be misleading. It will only calculate the files that it reaches. If a file is not reachable by the entry point that executes coverage, then VM has never seen it and therefore won’t return any coverage information.

Testing Unreached Files

There is an open issue on the Flutter repository to addresses this. But there is no official solution forit right now. Luckily, some community members came up with a workaround for this.

All you need to do is to add all the files to the starting point of the tests. This way, the VM knows which files are part of your ecosystem.

But you shouldn’t be entering all these details manually! Let’s write a script to import all relevant files. This is where you would be writing your script to add everything that is needed to import all the necessary files.

With the command above, you are creating a file under test folder called coverage_helper_test.dart. You will add the imports under it.

This script will be simply going through every file in the lib folder, determining relative file paths, and adding them as imports with the chosen package name.

One thing to mention here is, you would not want to have your generated files be part of your calculations. Because they are not something that you created. That is why the path is not added if the file is generated by a library.

Now you have a list of files that are provided for the VM.

Updating lcov Results

Now when you run the flutter test with coverage, it will have better coverage but still, it is going to show generated files that the test touches.

As it is stated above, you would not want to have generated files on your test results as you do not write them. :) You can remove them from the lcov.info the file that is created.

You can use the lcov library to do this.

This way your lcov.info is ready to be used for percentage generation.

BONUS: Uploading the results to Codecov via GitHub Actions

Codecov is one of the most famous code coverage websites out there. A lot of libraries like Bloc use it to show the code coverage and how it is distributed.

It is free for open-source projects. So if you are an OSS maintainer, it is a great option for you.

For starting off with this example, go ahead and create a file called gather_files.sh under the test folder.

Set up your GitHub Actions file and be sure to add the following.

All you need to do is to provide the lcov file that you created before and pass it to Codecov Action.

Once the action is running, it will update your project to Codecov and provide you a report.

As the last step, be sure that you are adding and removing the coverage helper file before and after your GitHub Action. You should not add the generated file to your VCS.

Now after this implementation, you will be able to get real coverage for your project. Go ahead and write amazing tests now!

Do not forget to follow me on Twitter!

--

--