Android Studio 1.4代码覆盖率

时间:2015-11-25 19:25:44

标签: android unit-testing gradle code-coverage jacoco

我在项目中获取代码覆盖率时遇到了一些麻烦。我的单元和UI测试放在“androidTest”文件夹中。测试运行顺利,但是当我尝试获取代码覆盖率(都使用覆盖率运行或者来自CLI的./gradlew createDebugCoverageReport)时,我会得到不同类型的错误。

这是我的build.gradle。

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

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {
            testCoverageEnabled = true
        }

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

    jacoco {
        version "0.7.1.201405082137"
    }
}

dependencies {
    compile 'junit:junit:4.12'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.+'
    compile 'com.android.support:cardview-v7:22.0.+'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.github.bmarrdev:android-DecoView-charting:v0.9.3'
    compile 'com.github.PhilJay:MPAndroidChart:v2.1.5'
    compile 'com.wefika:horizontal-picker:1.1.0'
    compile 'info.hoang8f:android-segmented:1.0.6'
    compile 'com.google.guava:guava:18.0'

    testCompile 'com.android.support.test:testing-support-lib:0.1'
    testCompile 'com.android.support:support-annotations:22.0.1'
    testCompile 'com.android.support.test:runner:0.4.1'
    testCompile 'com.android.support.test:rules:0.4.1'
    testCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    testCompile 'org.hamcrest:hamcrest-library:1.3'

    androidTestCompile 'com.android.support:support-annotations:22.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'

    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
}

请让我知道如何为“androidTest”文件夹中的InstrumentationTestCase和ActivityInstrumentationTestCase2设置代码覆盖率。

谢谢!

1 个答案:

答案 0 :(得分:0)

这就是我在build.gradle(app)文件中设置jacoco的方法。它没有任何问题,工作正常。设置是使用JUnit 4.12进行单元测试。希望这有帮助

layout_marginTop="-25"