Android Coverage launch with JaCoCo

半世苍凉 提交于 2020-01-11 12:37:52

问题


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).

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.

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?

Thanks!


回答1:


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('$$', '$'))
            }
        }
    }
}

// this is for the report

 debug {
            testCoverageEnabled true
        }

// this is for offline

add these to the build.gradle file.

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

add jacoco-agent.properties file to resources.

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();

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".

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

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



来源:https://stackoverflow.com/questions/30035964/android-coverage-launch-with-jacoco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!