Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Unit Testing and Code Coverage for Android

DZone's Guide to

Unit Testing and Code Coverage for Android

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Published at DZone with permission of Sai Geetha M N, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}