gpt4 book ai didi

android - 如何找到用于手动测试的android应用程序的代码覆盖率?

转载 作者:行者123 更新时间:2023-11-28 21:37:28 35 4
gpt4 key购买 nike

我想找到一个Android应用程序(Android studio + gradle)的手动测试和自动测试(appium)的代码覆盖率。

StackOverflow 上已经有一些与此问题相关的问题,但没有一个对我有用。我已经按照这个问题的步骤进行了:Android Coverage launch with JaCoCo在 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应用程序的代码覆盖率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56733960/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com