Search code examples
androidunit-testingantcode-coverageemma

How To do code coverage with emma during Android Unit Tests


You're looking for a simple way to do Android Unit Tests with code coverage and a report? Here you get a short HowTo.


Solution

  • First of all i assume that you have already a project and a test project with test cases. Also you have installed the Android sdk. The android testing framework has already a built-in ability to generate a code coverage report about your tests. This built-in ability is based on emma.

    Firs of all you've to create a build.xml for your project, which should be tested:

    android update project --path <Path to your Project>
    

    Next step is to create the build.xml for the test project (where the test cases are located):

    android update test-project -m <Path to your Project> -p <Path to your Testproject>
    

    In the next step you ccan run the coverage. Therefor you must switch into you Testproject:

    cd <Path to your Testproject>
    ant emma debug install test
    

    This will build, instrument, deploy and run your unit tests. It will also generate a HTML coverage report. The location of the report will be displayed in the last few lines of the Ant script’s output.

    For more Details: http://developer.android.com/tools/building/building-cmdline.html#AntReference

    Troubleshooting:

    • If your OS doesn't know the commands you have to add some paths to the environment path variable of you OS: you have to add the Path of the android.bat (normally /tools) and the path of the ant.bat (depending where you installed Ant; If you use Eclipse with ADT it is normally there: /plugins/org.apache.ant.../bin)
    • If you're using SDK 13 or older you've to use ant coverage instead of ant emma debug install test
    • The tests are running completely but there is no report (and there is also a "failed"-message which contains "permission denied"): You need to use a rooted device or an emulator
    • Generally it could help to clean your projects when you get a "BUILD FAILED"
    • If you have libraries that you use and you get an error. Make sure you put them into a "libs" directory. ant automatically adds the libraries in "libs" to the build path.