Code coverage in Android Studio

Code coverage in Android Studio

Code coverage is a metrics that helps you understand how much % of your source code has been tested in unit/integration tests that you have written in any language.

In Android studio it is pretty easy to get the code coverage and also know the lines that were covered and lines that were not covered in testing.

To get simple code coverage( assuming you have default studio settings). Just go to the class and click on the test/class containing the test and choose to run with code coverage option.

Run Code coverage from the class
Run Code coverage from the class

Then just have a look at the folder list.

Next to the class you will have code coverage percentage.

Next if you want to know Coverage of all your test cases, just goto Java folder in testing, right click and run test with coverage. That would help you to determine coverage for all test cases.

However it is not still easy to tell that if code coverage < 100%, what portion of code we missed.

However it is quite easy thanks to Android Studio. Just goto your configuration, that you have created for testing and choose tracing radio button and check “Track per test coverage”

 

Code Coverage in Android Studio
Code Coverage

Run the coverage

Run Code Coverage
Run Code Coverage

You see this window

You see the coverage

Code Coverage in %
Code Coverage in %

You can now see lines not covered in red and covered in green . Lines in red needs test cases to be exercised upon them.

Lines not covered
Lines not covered

If the colors are too light for your eyes you can always darken them by clicking on the red bar

Configure code coverage indicators
Configure code coverage indicators

And then choosing colors of your likening

Change Colors of code coverage
Change Colors of code coverage

 

Leave a Reply

Your email address will not be published. Required fields are marked *