Platinum Partner
java,mobile,tutorial

Unit Testing and Code Coverage for Android

This is a very small post for all those who have been struggling to generate the Android Test Coverage Using the ant scripts provided by the Android SDK.

I am assuming that you have created one project containing the actual source code to be tested - assume it is called "MyProject" and you have created another test project that tests MyProject, that is named MyProjectTest.

Then, you have written JUnit tests using the inherent testing support framework with Android. Now, you have run your unit tests and they run successfully in Eclipse. You then want to check on the code coverage of these unit tests. Here are the steps for the same:

  • At the command prompt, change directory to the MyProject folder
  • Rename the existing build.xml to build_orig.xml
  • Run this command:
    android update project -p . (including the last dot)
  • Then change directory to cd ..\MyProjectTest
  •  Rename the existing build.xml to build_orig.xml
  •  Run this command:      
    android update test-project -m D:/..../source/MyProject -p . 
     (including the last dot and where you give your complete path to the MyProject folder after -m)
  • Then, run the  command -
    ant emma debug install test
    

    This should generate a html report of the coverage under MyProjectTest/coverage

Troubleshooting:

In case you get an error "remote object coverage.ec does not exist" then start adb shell in another command prompt - in order to give write permissions to the sdcard for writing the coverage.ec file

Run the following command at the shell

mount -o remount rw /sdcard



Then you should be able to generate the report.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}