2012-05-31 14 views
2

我的主项目是一个android库。 我已经创建了一个测试项目,其中包含一些单元测试。 测试运行良好,但我努力获得测试覆盖率。 当我生成覆盖率报告时,我只能得到测试用例的覆盖范围 而不是库。 因此,它几乎100%,因为100%的测试正在运行。 但这并不能帮助我判断图书馆的哪些部分已经过测试。什么是检查android库测试覆盖率的正确方法?

这是我如何做到这一点的时刻:

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)失败,因为库项目不能安装

任何帮助将真的很感激!

回答

0

因此,这里是我找到了解决办法:

创建您的测试项目中的新的build.xml 复制“测试”的目标,从Android SDK中的build.xml文件(我用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> 
相关问题