Sai Geetha started off as a Java Developer and moved on to work as a Solution Architect and an Enterprise Architect. However, as a hobby she continues to don the hats of a Java Developer as well as an Android Developer. She loves to share what she learns with the larger community. In her interest to share her knowledge, she blogs Android tutorials at saigeethamn.blogspot.com and her other technical musings at sageethatechnical.blogspot.com. Sai Geetha is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

Unit Testing and Code Coverage for Android

04.30.2012
| 7152 views |
  • submit to reddit

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 Sai Geetha M N, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)