检查android库的测试覆盖率的正确方法是什么?

时间:2012-05-31 09:49:42

标签: android project code-coverage emma

我的主要项目是一个安卓库。 我已经创建了一个测试项目,其中包含一些单元测试。 测试运行良好,但我正在努力获得测试覆盖率。 当我生成覆盖率报告时,我只获得测试用例的覆盖范围, 但不是图书馆。 所以它几乎100%,因为100%的测试正在运行。 但这并没有帮助我说出图书馆的哪些部分已经过测试。

这就是我现在这样做的方式:

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

如果我不执行步骤2),则步骤3)失败,因为无法安装库项目

真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

所以这是我找到的解决方案:

在测试项目中创建一个新的build.xml 从android sdk build.xml文件中复制“test”目标(我使用过sdk20预览版2)

修改源路径以指向报告部分中的库项目

这是我现在使用的那个:

 <!-- 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>