How to use EMMA code coverage in android

Narendra picture Narendra · May 4, 2010 · Viewed 24.7k times · Source

I'm new to EMMA, I don't know how to use this for android system. Can anyone please give a sample for using this with android. Thanks a lot.

Answer

Grimmace picture Grimmace · Dec 17, 2011

I've only been able to get emma working using ant. If you've got an ant build set up then you can run:

ant emma debug install
ant emma debug install test

The first is run in your project directory, the second from your test directory. See the docs for more details: http://developer.android.com/guide/developing/building/building-cmdline.html

If you don't have an ant build.xml file already you can see how to generate one from your current project here: http://developer.android.com/guide/developing/projects/projects-cmdline.html

The sad part is this will only work on the emulator or a rooted device. This is because the coverage file gets generated in a folder that requires root. I also needed to modify the android-sdk/tools/ant/build.xml file to copy the file on my rooted device elsewhere so I could pull it off. I modified the emma block of the xml to be the following:

    <if condition="${emma.enabled}">
        <then>
            <echo>WARNING: Code Coverage is currently only supported on the emulator and rooted devices.</echo>
            <run-tests-helper emma.enabled="true">
                <extra-instrument-args>
                    <arg value="-e" />
                       <arg value="coverageFile" />
                       <arg value="${emma.dump.file}" />
                </extra-instrument-args>
            </run-tests-helper>
            <echo>Copying coverage to readable directory...</echo>
            <exec executable="${adb}" failonerror="true">
                <arg line="${adb.device.arg}" />
                <arg value="shell" />
                <arg value="echo cp ${emma.dump.file} /sdcard/coverage.ec | su" />
            </exec>
            <echo>Downloading coverage file into project directory...</echo>
            <exec executable="${adb}" failonerror="true">
                <arg line="${adb.device.arg}" />
                <arg value="pull" />
                <arg value="/sdcard/coverage.ec" />
                <arg value="coverage.ec" />
            </exec>
            <echo>Extracting coverage report...</echo>
            <emma>
                <report sourcepath="${tested.project.absolute.dir}/${source.dir}"
                                  verbosity="${verbosity}">
                    <!-- TODO: report.dir or something like should be introduced if necessary -->
                    <infileset dir=".">
                        <include name="coverage.ec" />
                        <include name="coverage.em" />
                    </infileset>
                    <!-- TODO: reports in other, indicated by user formats -->
                    <html outfile="coverage.html" />
               </report>
            </emma>
            <echo>Cleaning up temporary files...</echo>
            <delete file="coverage.ec" />
            <delete file="coverage.em" />
            <echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
        </then>
        <else>
            <run-tests-helper />
        </else>
    </if>