Image by Yu Kato through Unsplash

Pcov: better code coverage for Flutter

Thomas Middel


Testing can give you some great insights into the quality of your code, as well as provide you with a safety net when refactoring your code for example. Testing should be easy and fun, and with pcov things just got better for your Flutter projects!

The Flutter framework provides you with some great testing tools out of the box. You can easily run your tests using flutter test to see if all your tests are still passing. If you also want to have some insights on code coverage for your project, you can do that too by simply adding a coverage flag to the test command: flutter test --coverage. What is pcov solving then, and why did we create it in the first place?

Misleading code coverage

So, you decided you’re going to work on unit testing and take code coverage as one of your metrics to get some insights on that. You might already have a project with a couple of tests, so you run the flutter test --coverage command and see a coverage of 18%. Not great, but a good starting point at least!
When thinking about it again, that 18% sounds quite unexpected though. It’s a fairly large project and we hardly wrote any tests for it, then how can we get to 18% already?

Because flutter test --coverage only takes the files into account you actually touched in your tests!