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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}