Search code examples
androidprojectcode-coverageemma

What is the correct way to check the test coverage of an android library?


My main project is an android library. I have created a test project with some unit test in it. the tests run fine, but I am struggling to get the test coverage. When I generate the coverage report, I only get the coverage of the tests cases, but not of the library. So it gives me almost 100% because 100% of the test are running. but that doesn't help me to tell what part of the library has been tested.

This is how I do it at the moment :

0) my project looks like this:
myLibraryProject        <- this is my android library project
myLibraryProject/tests  <- this is my android test project

1) build.xml file : (from myLibraryProject/tests directory)
>android update test-project --path . -m ../

2) Modify the ant.properties file :
#tested.project.dir=../
android.library.reference.1=..

3) only then can I run :
ant emma debug install test

if I don't do step 2) then step 3) fails because the library project can not be installed

Any help would really be appreciated!


Solution

  • So here is the solution I found:

    Create a new build.xml in your test project copy the "test" target from android sdk build.xml file (I have used sdk20 preview 2 version)

    modify the sourcepath to point at the library project in the report section

    this is the one I use now :

     <!-- version-tag: custom -->
     <target name="test" depends="-test-project-check"
                description="Runs tests from the package defined in test.package property">
        <property name="test.runner" value="android.test.InstrumentationTestRunner" />
    
        <if condition="${project.is.test}">
        <then>
            <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
    
            <!-- Application package of the tested project extracted from its manifest file -->
            <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
                    expression="/manifest/@package" output="tested.project.app.package" />
        </then>
        <else>
            <!-- this is a test app, the tested package is the app's own package -->
            <property name="tested.project.app.package" value="${project.app.package}" />
        </else>
        </if>
    
        <property name="emma.dump.file" value="/data/data/${tested.project.app.package}/coverage.ec" /> 
    
        <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 level="info">Downloading coverage file into project directory...</echo>
                <exec executable="${adb}" failonerror="true">
                    <arg line="${adb.device.arg}" />
                    <arg value="pull" />
                    <arg value="${emma.dump.file}" />
                    <arg value="coverage.ec" />
                </exec>
                <echo level="info">Extracting coverage report...</echo>
                <emma>
                    <report sourcepath="../${source.dir}" verbosity="${verbosity}">
                        <sourcepath>
                          <dirset dir="${basedir}" >
                            <include name="../src" /> 
                            </dirset>
                        </sourcepath>
                        <infileset dir=".">
                            <include name="coverage.ec" />
                            <include name="coverage.em" />
                        </infileset>
                        <html outfile="coverage.html" />
                        <xml outfile="coverage/coverage.xml" />
                   </report>
                </emma>
                <echo level="info">Cleaning up temporary files...</echo>
                <delete file="coverage.ec" />
                <delete file="coverage.em" />
                <echo level="info">Saving the report file in ${basedir}/coverage/coverage.html and ${basedir}/coverage/coverage.xml</echo>
            </then>
            <else>
                <run-tests-helper />
            </else>
        </if>
    </target>