欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

集成Jacoco

程序员文章站 2022-07-09 19:52:11
...

在project中加上

classpath "org.jacoco:org.jacoco.core:0.7.9"

在app的gralde文件第一行下面加上

apply plugin: 'jacoco'

配置jacocoTestReport

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

    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

在Application的onCreate加上

createEcFile();
        new Thread() {
            @Override
            public void run() {
                super.run();
                while (true) {
                    writeEcFile();
                    try {
                        Thread.sleep(6000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
private void createEcFile() {
        Log.d(TAG, "create coverage file");
        String DEFAULT_COVERAGE_FILE_PATH = "/mnt/sdcard/" + getPackageName();
        String DEFAULT_COVERAGE_FILE = DEFAULT_COVERAGE_FILE_PATH + "/coverage.ec";
        File file_path = new File(DEFAULT_COVERAGE_FILE_PATH);
        File file = new File(DEFAULT_COVERAGE_FILE);
        Log.d(TAG, "file_path = " + file_path);
        if (!file.exists()) {
            try {
                file_path.mkdirs();
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
private void writeEcFile() {
        Log.d(TAG, "writeEcFile");

        OutputStream out = null;
        try {
            out = new FileOutputStream("/mnt/sdcard/" + getPackageName() + "/coverage.ec", true);
            Object agent = Class.forName("org.jacoco.agent.rt.RT")
                    .getMethod("getAgent")
                    .invoke(null);
            out.write((byte[]) agent.getClass().getMethod("getExecutionData", boolean.class)
                    .invoke(agent, false));
        } catch (Exception e) {
            Log.d(TAG, e.toString(), e);
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

运行installDebug 

集成Jacoco

可以点点App,让App走走代码。当然也可以不走,因为onCreate

执行createDebugCoverageReport

集成Jacoco

然后,在 /mnt/sdcard/ 加自己包名里中获取coverage.ec的文件,放到 app\build\outputs\code-coverage\connected。这里面可能有一个.ec的文件删除了就行。

执行jacocoTestReport

集成Jacoco

然后打开app目录下的build的reports文件夹下面有jacoco。

集成Jacoco

用浏览器打开

集成Jacoco

 

因为配置的地方多我先放一下配置完的样子

apply plugin: 'com.android.application'
apply plugin: 'jacoco'


def coverageSourceDirs = [
        '../app/src/main/java'
]
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.ec")

    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}


android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled false
            testCoverageEnabled = true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'




    }
}

 

 

 

参考资料:

jacoco仿emma实现统计手工(UI)测试覆盖率

 

 

 

相关标签: Jacoco