Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get code coverage in Android using Maven (android-maven-plugin)

I have an Android Maven project (let's call it parent_project) that contains various submodules: my_library_project, app_using_my_library_project, test_project and extra_lib.

So, the structure would be like this:

parent_project
   * my_library_project (Android Library Project)
   * app_using_my_library_project (Demo app that uses the Android Library Project)
   * test_project (Project containing the tests instrumented against app_using_my_library_project)
   * extra_lib

What I would like is to generate test coverage for my Android project using Maven (and not Ant, I am already able to generate code coverage reports using Ant, following these instructions: https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project).

I have no strong preference for the code coverage tool used but I would prefer EMMA, since seems the most common in the Android development world.

I am using android-maven-plugin (http://code.google.com/p/maven-android-plugin/) in its 3.0.0-alpha-12 version and I have already tried to put in the configuration of my parent's pom.xml the next:

<test>
  <coverage>true</coverage>
  <createreport>true</createreport>
</test>

But that does not produce the desired code coverage report.

So:

  • Is there any difference between the pom configuration for getting code coverage for a standard Java project and an Android project?
  • Do you know any example Android project using Maven that has code coverage?
  • Any hints on how to do this?
like image 343
Edu Zamora Avatar asked Oct 25 '11 10:10

Edu Zamora


People also ask

How to check code coverage in Maven?

To get code coverage reports in a Maven project, we first need to set up the JaCoCo Maven plugin for that project. By integrating the JaCoCo plugin, the results of the code coverage analysis can be reviewed as an HTML report. The current version of the JaCoCo-Maven plugin can be downloaded from the MVN Repository.

How to add JaCoCo plugin in Maven?

We can configure the JaCoCo Maven plugin by following these steps: Add the JaCoCo Maven plugin to the plugins section of our POM file. Configure the code coverage report for unit tests. Configure the code coverage report for integration tests.

What is JaCoCo Maven plugin used for?

In this article, we will show you how to use a JaCoCo Maven plugin to generate a code coverage report for a Java project. JaCoCo is an actively developed line coverage tool, that is used to measure how many lines of our code are tested.

What is Android test coverage?

Code coverage is the measurement of how much of your source code your tests actually touch. In Android development, you can generate test coverage reports locally using JaCoCo, and then remotely store them using Codecov.


1 Answers

If you're going to stick with maven, and want a plugin for maven that will do the code-coverage job, I think Cobertura is a better choice, as Emma stable last build is from 2005.

Although in "Android Application Testing Guide" (a recent book from June this year) they talk about Emma and demonstrate how to use it for testing, I think people stick to it, because it's needed to build Android from source (and if Google use it for their own OS development, it should be the best, right?).

If you're not fanatically bound to Maven, I strongly recommend to try Robotium. Robotium has full support for Activities, Dialogs, Toasts, Menus, and Context Menus. It also supports code coverage (Ant based though, for now) and some people recognize it as one of the leading testing platforms for Android.

Edit:

According to the Cobertura site, it supports code coverage in Maven 1 and Maven 2 environments. Although, you can find examples with Maven 3 also. A problem exists between pom configurations of Maven 2 and Maven 3. It seems for the reporting to work you have to basically move your old reporting plugins into the configuration section of the new maven-site-plugin. (See the article for details).

Another option is to try and use Sonar with Maven. Sonar has cobertura embedded (also options to embedd EMMA) and some people state that they had successfully reported code coverage, despite they had problems using the "stand-alone" cobertura plugin.

like image 61
hovanessyan Avatar answered Oct 22 '22 01:10

hovanessyan