【发布时间】:2017-02-14 06:11:05
【问题描述】:
目前,我正在研究 Android 上的 GUI 自动化测试,出于某种原因,我需要一个可以通过手动测试生成代码覆盖率报告的工具。
经过长时间的搜索,我发现Jacoco 和Emma 在他们的网站上提到了手动方法。
但是很遗憾,网上没有up-to-date-working example。
我尝试了很多建议的解决方案,例如https://groups.google.com/forum/#!searchin/jacoco/manual$20android%7Csort:date/jacoco/vx0g_6TKY8Q/0Tg3fX84CAAJ。
它生成了一个coverage.exec,但文件的大小只有几个字节(当然,Jacoco 无法从中生成任何报告。)
这是我尝试过的:https://github.com/kindraywind/MyDummy
在 app/build.gradle 中
apply plugin: 'jacoco'
jacoco {
toolVersion ="0.7.8+" //I did try "0.7.4+" as the suggest.
}
task jacocoTestReport(type: JacocoReport) { … }
在 jacoco-agent.properties 中
destfile=/storage/sdcard/coverage.exec
在 app/src/main/AndroidManifest.xml`中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在 MainActivity.java 中
protected void onStop()
{
super.onStop();
if(BuildConfig.DEBUG)
{
String TAG = "jacoco";
try {
String covPath = Environment.getExternalStorageDirectory().getPath() + "/coverage.exec";
File coverageFile = new File(covPath);
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",coverageFile.getClass(), boolean.class, boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, true, false);
} catch (Exception e) {
}
}
}
模拟器是 Nexus 5 API 19(我确实尝试了大多数版本。)
来自设备的日志 EMMA:运行时覆盖数据合并到 [/storage/sdcard/coverage.exec] {in 8 ms}
运行后的日志 ./gradlew jacocoTestReport
Unable to read execution data file /Users/MyDummy/app/coverage.exec
如果相关,我正在使用 OSX10.12.3。
总而言之,我需要知道(或任何工作示例)如何在以下情况下获得代码覆盖率:
- 手动测试应用程序。
- 在 Android 应用程序上。
- 使用 Gradle 而不是 Maven 或 Ant。
- Android Studio 不是 Eclipse。
我看不到出路,非常感谢您的帮助。
【问题讨论】:
标签: android android-studio code-coverage manual-testing