How to get code coverage using Android Studio?

I am developing applications using Android Studio.
I managed to run the test code.
But I don’t know how to get code coverage in android studio.

I have already seen the following links.
Android Gradle Code Coverage
But I can’t wait for the upgrade to version v0.6 that supports emma.

The configuration of the project is as follows.

Main code
MyProject / AppName / SRC / Primary / Java / MyPackage / MyClass.java

Test code
MyProject / AppName / SRC / instrumentTest / Java / MyPackage / test / MyClassTest.java

Project configuration
Myproject
├─build.gradle
└─AppName
├─build.gradle
└─src
├─main
│ ├─java
│ │ └─mypackage
│ │ └─MyClass.java
│ ├─res
│ └─AndroidManifest.xml
└─instrumentTest
└─java
└─mypackage
└─test
└─ MyClassTest.java

+63
android android-studio code-coverage gradle
Sep 08 '13 at 11:14
source share
6 answers

With the new version of Android Studio 1.2, 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.




In Android Studio 2.1.3, the label is Run Unit tests with Coverage , where Unit test is the name of your test configuration, as shown in the following screenshot:

Android Studio:

+58
May 11 '15 at 21:08
source share

There are so many answers that show how to apply the jacoco plugin to an Android studio project that is outdated and spent a lot of time finding a solution for the recently Android studio (My Android Studio - version 2.1.2).

  • jacoco plugin is built-in for Android Studio gradle, you just need to enable it as shown below:
   buildTypes {
     ...
     debug {
       testCoverageEnabled true
     }
   }
  • After completing the above, complete the unit test ./gradlew testDebugUnitTest

  • Then create coverage files: ./gradlew createDebugCoverageReport

  • Protection files will be created in the <module>/build/reports/coverage/debug folder, include index.html , which you can open in your browser, and report.xml , which you can use to get a jenkins jacoco plugin report or another one that continues integration instruments.

For those who get 0% coverage with the jenkins jacoco plugin , be sure to use the correct version. Quote from their website :

Unfortunately, JaCoCo 0.7.5 breaks compatibility with the previous binary jacoco.exec file formats. The JaCoCo plugin prior to version 1.0.19 is based on JaCoCo 0.7.4, so you cannot use this version with projects that already use JaCoCo 0.7.5 or later. Starting from version 2.0.0, the JaCoCo plugin uses JaCoCo 0.7.5 and therefore also requires this version to be used in your projects. Please stick with JaCoCo plugin 1.0.19 or lower if you are still using JaCoCo 0.7.4 or lower

+30
Sep 01 '16 at 4:50
source share

Have you tried using the Jacoco plugin to get code coverage for your project? This is a good plugin that gives you coverage based on your package or individual classes. I'm not sure how you configure Jacoco to use with Gradle, since I'm using Maven. Check the link : and see if it helps you

+2
Dec 27 '13 at 2:00
source share

We use maven to create our application and coberra for code coverage reports.

both integrate very easily

Android maven integration:

http://www.vogella.com/tutorials/AndroidBuildMaven/article.html

Maven + Cobertura code coverage example Example:

http://www.mkyong.com/qa/maven-cobertura-code-coverage-example/

+2
Apr 24 '14 at 19:17
source share

I don’t think you can see the visual code coverage report inside Android Studio. But you can try Jacoco . You will need to integrate it into your build.gradle file. You can find a similar question and solution here.

+2
Jun 15 '14 at 15:48
source share

Android studio gradle has a built-in Jacoco plugin that you can use to find code coverage. I wrote an article to set up jaococo step by step to find code coverage for the Espresso test case, but you can also use it for Robotium. check this.

http://qaautomated.blogspot.in/2016/03/how-to-find-code-coverage-with-jacoco.html

+2
Mar 16 '16 at 7:25
source share



All Articles