【问题标题】:How to find the code coverage of an android application for manual testing?如何找到用于手动测试的 android 应用程序的代码覆盖率?
【发布时间】:2019-06-24 09:47:39
【问题描述】:

我想查找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);
        }
    }
}

【问题讨论】:

    标签: android testing gradle code-coverage jacoco


    【解决方案1】:

    在 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。如果模拟器上的覆盖率为 37 字节,请在真机上尝试或构建 APK 并将其放入模拟器中进行安装。

    然后您可以将 coverage.exec 拉入您的计算机并使用 jacoco 从中生成 HTML 报告。

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 2010-10-26
      • 2023-04-07
      • 2021-09-24
      相关资源
      最近更新 更多