JaCoCo推出Android Coverage [英] Android Coverage launch with JaCoCo

查看:149
本文介绍了JaCoCo推出Android Coverage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用Gradle/Android Studio构建的Android应用程序,并使用JaCoCo为我们的单元测试生成代码覆盖率报告;这很好.我们也对能够生成手动测试的覆盖率报告感兴趣;也就是说,显示在任意应用程序启动中包含哪些代码. JaCoCo的前任 EclEmma 似乎可以做到这一点,但我一直找不到关于JaCoCo的一种或另一种确认方式(尽管我开始认为缺乏话语是不可能的).

We have an Android application that we are building with Gradle/Android Studio and are using JaCoCo to generate code coverage reports for our unit tests; this is working great. We are also interested in being able to generate coverage reports for manual tests as well; that is, show what code was covered in an arbitrary application launch. It appears that JaCoCo's predecessor EclEmma was capable of this, but I have not been able to find any confirmation one way or the other about JaCoCo (though I am beginning to assume it impossible from the lack of discourse).

我曾尝试使用Eclipse中的EclEmma来获取某些内容,但最新版本失败,并出现

I have tried using EclEmma from Eclipse just to have something, but the latest version fails with this error, and I couldn't immediately get older versions to work either.

任何人都可以确认是否有可能通过JaCoCo在任意应用程序启动时生成覆盖率数据吗?在开始时,运行该应用程序,按下按钮,关闭该应用程序,并获得有关所按下的按钮执行了什么代码的报告.如果没有,是否还有另一种工具可以做到这一点?

Can anyone confirm whether or not it is possible to generate coverage data on an arbitrary application launch with JaCoCo? As in, run the app, press buttons, close the app and get a report on what code was exercised by the buttons you pushed. If not, is there another tool that can accomplish this?

谢谢!

推荐答案

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("enter code here"
            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('$$', '$'))
            }
        }
    }
}

//这是给报告的

 debug {
            testCoverageEnabled true
        }

//这是离线版本

将它们添加到build.gradle文件中.

add these to the build.gradle file.

将目录资源"添加到app> src> main

add directory "resources" to the app>src>main

将jacoco-agent.properties文件添加到资源中.

add jacoco-agent.properties file to resources.

将destfile =/sdcard/coverage.exec写入jacoco-agent.properties文件

write destfile=/sdcard/coverage.exec to file jacoco-agent.properties

现在将此类添加到您的项目中.

now add this class to your project .

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) {
            new Throwable("Is emma jar on classpath?", e);
        }
    }
}

当您的应用处于打开状态时,调用该函数

when your app is onDestroy call the function

jacoco.generateCoverageReport();

jacoco.generateCoverageReport();

您可以运行您的应用程序.当测试结束时,您可以使用命令 "adb pull/sdcard/coverage.exec yourapp/build/outputs/code-coverage/connected/coverage.exec".

you can run your app . when test over you can use command "adb pull /sdcard/coverage.exec yourapp/build/outputs/code-coverage/connected/coverage.exec".

上面定义的最后一个运行gradle任务"jacocoTestReport".

the last operation run gradle task define above there "jacocoTestReport".

好.全部完成.在"yourapp/build/reports/jacoco/jacocoTestReport/html/"中打开index.html.

ok. all done. open the index.html in "yourapp/build/reports/jacoco/jacocoTestReport/html/".

这篇关于JaCoCo推出Android Coverage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆