Code coverage in Android Studio

lage picture lage · Dec 5, 2014 · Viewed 9.6k times · Source

I have added testCoverageEnabled=true in my build.gradle file in order to start getting some code coverage reports.

Running my tests I can see that a code-coverage folder is added in my build outputs folders. However, when a reach its contents I see there is only a coverage.ec file...

What can I do with this file in order to get a recent report? Or what should I add to my build.gradle in order to get the full report?

Answer

Caleb picture Caleb · May 11, 2015

(EDIT: My old answer used Jacoco to get code coverage. With Android Studio's latest updates, you can get code coverage without third party tools)

With the new Android Studio, you are able to run your unit tests and see the coverage all within the IDE.

First, you'll need to get your unit tests running in the IDE. (if you already can, then skip this step)

This guide and demo will help you.

Secondly, you'll need to create a JUnit Run configuration

enter image description here

Inside this configuraiton, you'll be able to choose

  • Test Kind: "All in Package"
  • Package: [the package where your tests reside, eg: "com.myapp.tests"]
  • Search for tests: Across Module Dependencies (could be diff for your setup)
  • VM -options: -ea
  • Working Directory: [your project's directory]
  • Use classpath of mod: [select your module]

If you have any issue creating your JUnit Run Configuration, you should visit this guide for help.

Lastly, in the latest Android Studio, you should be able to run your JUnit-Run Configuration by clicking on the 'Run with Coverage' button.