Unit test coverage for flutter application in Android studio/Intellij

Raju Gupta
3 min readAug 5, 2021

Unit test is one of the most important topic in software engineering. Unit testing is handy for verifying the behavior of a single function, method or class. By practicing this developer can have less error in coding and also it will double check the code.

In this article we will see how can we check the code coverage. But you may be wondering what is code coverage and why do we need this?

Code coverage basically show you how much of your code is actually being used by your unit tests. Running a code coverage report helps show what code is not being used to help you write more unit tests. Code coverage can also show which branches in conditional logic are not being covered. Code coverage is calculated for a file based on the unit tests that SUCCESSFULLY passed.

Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

Let’s start

The first thing we need a plugin named flutter_enhancement_suite.

It is an Android Studio/IntelliJ plugin which generates coverage reports. It shows the coverage per file and also highlights covered lines.

Sample test coverage snapshot

To download this plugin, Settings> Plugins > Marketplace Tab> Search for Flutter Enhancement Tab

Flutter Enhancement Suite

Crate a new run configuration for testing coverage. To do so click on run tab. Run>Edit Configurations

Click ‘+’ to add new configuration.

Sample Test Configuration

Give the test name such as ‘All Test’ and set the scope. You can change it any time. Set the entry point. Entry point is the file test will start executing.

Now we are set to test the coverage.

Click on highlighted icon

Now click on the highlighted icon to run the test with coverage.

After successful test, you will see the coverage percentage of file and code on the lib folder as below.

Sample test coverage image

Now you can open any file and see the stats. Red color is for uncovered . Green color is for covered and gray color is for partially covered.

Please clap if I helped you :)

References:

Thanks for reading :)

--

--

Raju Gupta

I am a flutter developer. Writing blogs on the most difficult and intersting topic in flutter and dart. To know more about me mail @kraju751@gmail.com