代码之家  ›  专栏  ›  技术社区  ›  Michael Krussel

覆盖率报告称,android与gradle的覆盖率为零

  •  6
  • Michael Krussel  · 技术社区  · 10 年前

    我一直无法在我的任何android项目上获得代码覆盖率。

    为了简化工作,我创建了一个新项目(选择了空活动)。 在src/main/java中为项目添加了一个新的实用程序类。

    然后我在src/androidTest/java中为它创建了一个单元测试。

    更新了渐变文件以启用覆盖。

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"
    
        defaultConfig {
            applicationId "com.example.michaelkrussel.coverageapp"
            minSdkVersion 15
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
    
        jacoco {
            version "0.7.1.201405082137"
        }
    
        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:21.0.3'
    }
    

    我使用运行了测试/gradlew创建DebugCoverageReport。单元测试显示我创建的测试通过了,但覆盖率报告报告的覆盖率为零。

    我假设我要么在grade文件中丢失了什么,要么没有运行正确的grade任务,但我无法确定是什么。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Anderson K    10 年前

    我在我的项目中使用了这种配置,工作很好!

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.1"
    
        defaultConfig {
            applicationId "br.com.acs.app"
            minSdkVersion 18
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
            testApplicationId "br.com.acs.app.test"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
                testCoverageEnabled true
            }
        }
    
        packagingOptions {
            exclude 'LICENSE.txt'
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
    
        androidTestCompile 'junit:junit:4.11'
        androidTestCompile('com.android.support.test:testing-support-lib:0.1') {
            exclude group: 'junit' 
        }
    }