Code Coverage in Android Studio

I added testCoverageEnabled = true to the build.gradle file to start getting some code coverage reports.

Running my tests. I can see that a code coverage folder has been added to my build pin folders. However, when it reaches its contents, I see that there is only a coverage.ec file ...

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

+6
source share
2 answers

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

With the new Android studio, you can run your unit tests and view coverage in the IDE.

First, you will need to run your unit tests in the IDE. (if you can already, skip this step)

This guide and demo will help you.

Secondly, you need to create a JUnit Run configuration

enter image description here

Inside this configuration you can choose

  • Type of test: "Everything in the package"
  • Package: [package where your tests are located, for example: "com.myapp.tests"]
  • Search for tests: Interdependence between modules (may be the difference for your settings)
  • VM -options: -ea
  • Working directory: [directory of your project]
  • Use mod class path: [select your module]

If you are having trouble creating a JUnit Run configuration, you should contact this for help.

Finally, in the latest version of Android Studio, you can launch your JUnit-Run Configuration by clicking the "Run with Coverage" button.

+2
source

Add the following to the build.gradle file:

apply plugin: "jacoco" 

Run the test using

 gradlew :<module>:createDebugCoverageReport 

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

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

+1
source

Source: https://habr.com/ru/post/979182/


All Articles