简体   繁体   English

Android Espresso的空Jacoco报告

[英]Empty Jacoco report for Android Espresso

I am trying to get code coverage for my Android project using Espresso tests. 我正在尝试使用Espresso测试为我的Android项目获取代码覆盖率。 However, Jacoco is giving me back a report saying that I don't cover anything. 然而,Jacoco正在给我一份报告说我没有报道任何事情。 I made a demo app to highlight my problem and that is here . 我做了一个演示应用程序来突出我的问题,就在这里

If you don't want to go to Github to look at the project, here is the build.gradle file: 如果你不想去Github查看项目,这里是build.gradle文件:

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

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "ninja.bryansills.jacocotest"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'

    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
}

Based on the issue pointed out by Ligol, here is what worked for me. 根据Ligol指出的问题,这对我有用。

Added custom test runner in androidTest 在androidTest中添加了自定义测试运行器

package com.example;

import android.os.Bundle;
import android.support.test.runner.AndroidJUnitRunner;
import android.util.Log;

import java.lang.reflect.Method;

public class AndroidJacocoTestRunner extends AndroidJUnitRunner {

    static {
        System.setProperty("jacoco-agent.destfile", "/data/data/"+BuildConfig.APPLICATION_ID+"/coverage.ec");
    }

    @Override
    public void finish(int resultCode, Bundle results) {
        try {
            Class rt = Class.forName("org.jacoco.agent.rt.RT");
            Method getAgent = rt.getMethod("getAgent");
            Method dump = getAgent.getReturnType().getMethod("dump", boolean.class);
            Object agent = getAgent.invoke(null);
            dump.invoke(agent, false);
        } catch (Throwable e) {
            Log.d("JACOCO", e.getMessage());
        }
        super.finish(resultCode, results);
    }
}

Used this test runner in app/build.gradle 在app / build.gradle中使用此测试运行器

android{
    ...
    defaultConfig {
      ....
      testInstrumentationRunner "com.example.AndroidJacocoTestRunner"
    }
}

Your problem might be related to this issue. 您的问题可能与此问题有关。

https://code.google.com/p/android/issues/detail?id=170607 https://code.google.com/p/android/issues/detail?id=170607

我在三星设备上遇到了同样的问题,我没有改变任何东西,但改为HTC设备,我开始得到覆盖。用JaCoCo三星设备似乎有点可疑。

I advise everyone not to use a Samsung device while generating coverage reports. 我建议大家在生成报道时不要使用三星设备。 I tried everything to fix the 0 coverage issue and luckily changed my device to Redmi Note 5 Pro and voila the coverage starts showing. 我尝试了一切来解决0覆盖问题并幸运地将我的设备更改为Redmi Note 5 Pro并且瞧瞧覆盖范围开始显示。 On doing some research and reading some articles I found Samsung give less freedom to its user to use their products for testing. 在做一些研究和阅读一些文章时,我发现三星给用户提供了更少的自由来使用他们的产品进行测试。 Though you can do the testing on Samsung devices after rooting them. 虽然你可以在生根后对三星设备进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM