如何找到 android 应用程序的代码覆盖率以进行手动测试?
How to find the code coverage of an android application for manual testing?
我想查找 Android 应用程序 (Android studio + gradle) 的手动测试和自动测试 (appium) 的代码覆盖率。
Whosebug 上已经有一些与此问题相关的问题,但其中 none 对我有用。我已经按照这个问题的步骤进行了:
sdcard/ 中正在生成的 jacoco.exec 文件是 37 字节,基本上是空的。
build.gradle的当前配置:
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
def coverageSourceDirs = [
'../app/src/main/java'
]
jacoco{
toolVersion = "0.7.4.201502262128"
}
task jacocoTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/outputs/code-coverage/connected/coverage.exec")
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
// this is for the report
...
jacoco.java:
package anubhav.calculatorapp;
import android.util.Log;
import java.lang.reflect.Method;
public class jacoco {
static void generateCoverageReport() {
String TAG = "jacoco";
// use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
String coverageFilePath = "/sdcard/coverage.exec";
java.io.File coverageFile = new java.io.File(coverageFilePath);
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
coverageFile.getClass(), boolean.class, boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, false, false);
Log.e(TAG, "generateCoverageReport: ok");
} catch (Exception e) {
Log.e("jacoco", "inside catch. no emma jar found");
new Throwable("Is emma jar on classpath?", e);
}
}
}
将这些更改写入 build.gradle 文件:
apply plugin: 'jacoco'
另加
testCoverageEnabled true
到 buildTypes 中的调试 {}。
我无法强调 testCoverageEnabled 的重要性,它检测文件,没有它您将无法获得覆盖。确保此行添加正确。
要正确设置 'build.gradle',请检查 'build/intermediates'。
为AndroidManifest.xml
添加读写外部存储权限
在 MainActivity.java 的 onDestroy() 函数中添加这些行:
Log.d("StorageSt", Environment.getExternalStorageState());
String coverageFilePath = Environment.getExternalStorageDirectory() + File.separator+ "coverage.exec";
File coverageFile = new File(coverageFilePath);
super.onStop();
if(BuildConfig.DEBUG)
{
try {
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) {}
运行 您的应用,您会在 /sdcard/ 中找到 coverage.exec。如果模拟器上的覆盖范围是 37bytes,请在真实设备上尝试或构建 APK 并将其放入模拟器中进行安装。
然后您可以将 coverage.exec 拉入您的计算机并使用 jacoco 从中生成 HTML 报告。
我想查找 Android 应用程序 (Android studio + gradle) 的手动测试和自动测试 (appium) 的代码覆盖率。
Whosebug 上已经有一些与此问题相关的问题,但其中 none 对我有用。我已经按照这个问题的步骤进行了:
build.gradle的当前配置:
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
def coverageSourceDirs = [
'../app/src/main/java'
]
jacoco{
toolVersion = "0.7.4.201502262128"
}
task jacocoTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/outputs/code-coverage/connected/coverage.exec")
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
// this is for the report
...
jacoco.java:
package anubhav.calculatorapp;
import android.util.Log;
import java.lang.reflect.Method;
public class jacoco {
static void generateCoverageReport() {
String TAG = "jacoco";
// use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
String coverageFilePath = "/sdcard/coverage.exec";
java.io.File coverageFile = new java.io.File(coverageFilePath);
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
coverageFile.getClass(), boolean.class, boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, false, false);
Log.e(TAG, "generateCoverageReport: ok");
} catch (Exception e) {
Log.e("jacoco", "inside catch. no emma jar found");
new Throwable("Is emma jar on classpath?", e);
}
}
}
将这些更改写入 build.gradle 文件:
apply plugin: 'jacoco'
另加
testCoverageEnabled true
到 buildTypes 中的调试 {}。
我无法强调 testCoverageEnabled 的重要性,它检测文件,没有它您将无法获得覆盖。确保此行添加正确。
要正确设置 'build.gradle',请检查 'build/intermediates'。
为AndroidManifest.xml
添加读写外部存储权限在 MainActivity.java 的 onDestroy() 函数中添加这些行:
Log.d("StorageSt", Environment.getExternalStorageState());
String coverageFilePath = Environment.getExternalStorageDirectory() + File.separator+ "coverage.exec";
File coverageFile = new File(coverageFilePath);
super.onStop();
if(BuildConfig.DEBUG)
{
try {
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) {}
运行 您的应用,您会在 /sdcard/ 中找到 coverage.exec。如果模拟器上的覆盖范围是 37bytes,请在真实设备上尝试或构建 APK 并将其放入模拟器中进行安装。
然后您可以将 coverage.exec 拉入您的计算机并使用 jacoco 从中生成 HTML 报告。