我的主要项目是一个 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) 会失败,因为无法安装库项目
非常感谢任何帮助!
最佳答案
所以这是我找到的解决方案:
在你的测试项目中新建一个build.xml 从 android sdk build.xml 文件中复制“测试”目标(我使用的是 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>
关于android - 检查 android 库的测试覆盖率的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831209/