如何在android中使用EMMA代码覆盖率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2762665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use EMMA code coverage in android
提问by Narendra
I'm new to EMMA, I don't know how to use this for android system. Can anyone please give a sample for using this with android. Thanks a lot.
我是 EMMA 的新手,我不知道如何将它用于 android 系统。任何人都可以提供一个在android上使用它的示例。非常感谢。
回答by Grimmace
I've only been able to get emma working using ant. If you've got an ant build set up then you can run:
我只能让 emma 使用 ant 工作。如果您已经设置了 ant 构建,那么您可以运行:
ant emma debug install
ant emma debug install test
The first is run in your project directory, the second from your test directory. See the docs for more details: http://developer.android.com/guide/developing/building/building-cmdline.html
第一个在您的项目目录中运行,第二个在您的测试目录中运行。有关更多详细信息,请参阅文档:http: //developer.android.com/guide/developing/building/building-cmdline.html
If you don't have an ant build.xml file already you can see how to generate one from your current project here: http://developer.android.com/guide/developing/projects/projects-cmdline.html
如果您还没有 ant build.xml 文件,您可以在此处查看如何从当前项目生成一个文件:http: //developer.android.com/guide/developing/projects/projects-cmdline.html
The sad part is this will only work on the emulator or a rooted device. This is because the coverage file gets generated in a folder that requires root. I also needed to modify the android-sdk/tools/ant/build.xml file to copy the file on my rooted device elsewhere so I could pull it off. I modified the emma block of the xml to be the following:
可悲的是,这只适用于模拟器或有根设备。这是因为覆盖文件是在需要 root 的文件夹中生成的。我还需要修改 android-sdk/tools/ant/build.xml 文件以将我的 root 设备上的文件复制到其他地方,以便我可以将其关闭。我将 xml 的 emma 块修改为以下内容:
<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>Copying coverage to readable directory...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="shell" />
<arg value="echo cp ${emma.dump.file} /sdcard/coverage.ec | su" />
</exec>
<echo>Downloading coverage file into project directory...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="pull" />
<arg value="/sdcard/coverage.ec" />
<arg value="coverage.ec" />
</exec>
<echo>Extracting coverage report...</echo>
<emma>
<report sourcepath="${tested.project.absolute.dir}/${source.dir}"
verbosity="${verbosity}">
<!-- TODO: report.dir or something like should be introduced if necessary -->
<infileset dir=".">
<include name="coverage.ec" />
<include name="coverage.em" />
</infileset>
<!-- TODO: reports in other, indicated by user formats -->
<html outfile="coverage.html" />
</report>
</emma>
<echo>Cleaning up temporary files...</echo>
<delete file="coverage.ec" />
<delete file="coverage.em" />
<echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
</then>
<else>
<run-tests-helper />
</else>
</if>
回答by plainjimbo
Apparently EMMA isn't supported for the SDK Yet. It's only supported for Apps and Tests that are part of the source tree. Which seems weird because they don't mention this in the dev-guide where they tell you about the EMMA instrumentation options. http://developer.android.com/guide/developing/testing/testing_otheride.html#AMOptionsSyntax
显然,SDK 尚不支持 EMMA。它仅支持作为源树一部分的应用程序和测试。这看起来很奇怪,因为他们没有在开发指南中提到这一点,他们告诉您有关 EMMA 检测选项的信息。 http://developer.android.com/guide/developing/testing/testing_otheride.html#AMOptionsSyntax
You can read the response from this guy (who appears to be on the dev team? I dunno) http://groups.google.com/group/android-developers/msg/a542afd318832371
你可以阅读这个人的回复(他似乎在开发团队?我不知道) http://groups.google.com/group/android-developers/msg/a542afd318832371
If you want to download and build the Android Source it sounds feasible to get EMMA working for your own App. However, it sounds kind of tricky and possibly a little unstable. See the full thread for more info: http://groups.google.com/group/android-developers/browse_thread/thread/43cf8a8ca5662f85/9b68eec3e7b625f3?lnk=gst&q=Emma#
如果您想下载和构建 Android 源,让 EMMA 为您自己的应用程序工作听起来是可行的。然而,这听起来有点棘手,可能有点不稳定。有关详细信息,请参阅完整线程:http: //groups.google.com/group/android-developers/browse_thread/thread/43cf8a8ca5662f85/9b68eec3e7b625f3?lnk=gst&q=Emma#
回答by Luigi Agosti
If you are using maven for your project is fairly simple with rooted devices check this wiki :
如果您在项目中使用 maven 并使用根设备检查这个 wiki 是相当简单的:
http://code.google.com/p/maven-android-plugin/wiki/EmmaMaven
http://code.google.com/p/maven-android-plugin/wiki/EmmaMaven
it was easy to implement the solution apart the report generation but if you check the comments of the wiki you will find the solution
除了报告生成之外,很容易实现解决方案,但是如果您查看维基的评论,您会找到解决方案