2012-05-31 15 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> 
相關問題