【问题标题】:What is the correct way to check the test coverage of an android library?检查android库的测试覆盖率的正确方法是什么?
【发布时间】:2012-05-31 09:49:42
【问题描述】:

我的主要项目是一个 android 库。 我创建了一个包含一些单元测试的测试项目。 测试运行良好,但我正在努力获得测试覆盖率。 当我生成覆盖率报告时,我只得到测试用例的覆盖率, 但不是图书馆的。 所以它给了我几乎 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) 将失败,因为无法安装库项目

任何帮助将不胜感激!

【问题讨论】:

    标签: android project code-coverage emma


    【解决方案1】:

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

    在您的测试项目中创建一个新的 build.xml 从 android sdk build.xml 文件中复制“test”目标(我使用的是 sdk20 preview 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-08
      • 2012-09-12
      • 2023-03-11
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      相关资源
      最近更新 更多