Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage in Android Studio

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?

like image 328
lage Avatar asked Dec 05 '14 16:12

lage


2 Answers

(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.

like image 56
Caleb Avatar answered Oct 21 '22 17:10

Caleb


Add the following to the "build.gradle" file

apply plugin: "jacoco"

Run the test using

gradlew :<module>:createDebugCoverageReport

Run the command from the project root replacing "module" with the name of the module under test.

The output should be in the module under "build/outputs/reports/coverage"

like image 37
AndroidGuy Avatar answered Oct 21 '22 16:10

AndroidGuy