多模块 Android 项目中的全局代码覆盖率:合并代码覆盖率报告(单元和 UI 测试)

Global code coverage in multi-module Android project: merge code coverage reports (Unit & UI tests)

我有一个 Android 应用程序,它由 2 个模块组成:

对于他们每个人,我都有一个 gradle 任务来验证代码覆盖率:

作为客户的要求,我需要合并这两个报告以获得应用程序的 overall/global 代码覆盖率

注意:我使用的是 Gradle 版本 3.1.2.


应用Gradle文件:

apply plugin: 'jacoco'

android {

   testBuildType "uiTest"

    ...

  buildTypes {
    debug {
        applicationIdSuffix ".debug"
        versionNameSuffix "-debug"
        debuggable true

        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


        matchingFallbacks = ['debug']
    }

    // TESTS

    // unitTest will be used to run unit tests.
    unitTest.initWith(buildTypes.debug) //Beware the buildType this depends on MUST be above on the gradle file
    unitTest {
        applicationIdSuffix ".unitTest"
        versionNameSuffix "-unitTest"

        testCoverageEnabled = true
        matchingFallbacks = ['unitTest', 'debug']
    }

    // uiTest will be used to run ui tests.
    uiTest.initWith(buildTypes.debug) //Beware the buildType this depends on MUST be above on the gradle file
    uiTest {
        applicationIdSuffix ".uiTest"
        versionNameSuffix "-uiTest"

        testCoverageEnabled = true
        matchingFallbacks = ['uiTest', 'debug']
    }

    ...

子模块 Gradle 文件:

apply plugin: 'jacoco'

android {

   testBuildType "uiTest"

   buildTypes {
    debug {
    }

    unitTest {
        initWith(buildTypes.debug)
        testCoverageEnabled = true
    }

    uiTest {
        initWith(buildTypes.debug)
        testCoverageEnabled = true
    }

  ...
}

我尝试了几种方法,下面的这个确实合并了测试..但是覆盖率显示不正确:

在应用中创建UI测试覆盖率的任务:

//UI 测试覆盖率过滤(我们需要运行 App 的单元测试才能使用 Jacoco 进行过滤)

task createTestReport(type: JacocoReport, dependsOn: [':app:testUnitTestUnitTest', ':app:createUiTestAndroidTestCoverageReport']) {

reports {
    html.enabled = true
}

def fileFilter = [
        //Android stuff
        '**/R.class',
        '**/BR.class',
        '**/R$*.class',
        '**/BR$*.class',
        '**/BuildConfig.*',
        'android/**/*.*',
        //Data Binding
        '**/*databinding',
        '**/*binders',
        '**/*layouts',
        '**/Manifest*.*',
        '**/*Test*.*',
        "**/services/**/model/**",
        //Utils
        '**/utils/*.*',
        '**/utils/**/*.*'
]

//To support Java coverage on Unit tests
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/unitTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/unitTest", excludes: fileFilter)

def mainSrc = "${project.projectDir}/src/main/java"
def debugSrc = "${project.projectDir}/src/debug/java"

sourceDirectories = files([mainSrc, debugSrc])


def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code-coverage/connected/", includes: ["**/*.ec"])
def appOtherAndroidTests = fileTree(dir: "${buildDir}/outputs/androidTest-results/connected/", includes: ["**/*.ec"])

classDirectories = files([debugTree], [kotlinDebugTree])
executionData = files("${buildDir}/jacoco/testUnitTestUnitTest.exec", appAndroidTests, appOtherAndroidTests)

}

在子模块中创建单元测试覆盖率的任务:

//过滤单元测试覆盖率

任务 createTestReport(类型:JacocoReport,取决于:['testUnitTestUnitTest']){

reports {
    html.enabled = true
}

def fileFilter = ['**/R.class',
                  '**/BR.class',
                  '**/R$*.class',
                  '**/BR$*.class',
                  '**/BuildConfig.*',
                  '**/*databinding/**/*.*',
                  '**/Manifest*.*',
                  '**/*Test*.*',
                  "**/services/**/model/**",
                  'android/**/*.*',
                  '**/utils/*.*',
                  '**/utils/**/*.*']

//To support Java coverage on Unit tests
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/unitTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/unitTest", excludes: fileFilter)

def mainSrc = "${project.projectDir}/src/main/java"
def debugSrc = "${project.projectDir}/src/debug/java"

sourceDirectories = files([mainSrc, debugSrc])


classDirectories = files([debugTree], [kotlinDebugTree])
executionData = files("${buildDir}/jacoco/testUnitTestUnitTest.exec")

}

在应用程序中创建全球覆盖的任务:

//全球测试覆盖率

任务 createGlobalTestReport(类型:JacocoReport, dependsOn: [':app:testUnitTestUnitTest', ':app:createTestReport', ':submodule:testUnitTestUnitTest']) {

reports {
    html.enabled = true
}

def fileFilter = [
        //Android stuff
        '**/R.class',
        '**/BR.class',
        '**/R$*.class',
        '**/BR$*.class',
        '**/BuildConfig.*',
        'android/**/*.*',
        //Data Binding
        '**/*databinding',
        '**/*binders',
        '**/*layouts',
        '**/Manifest*.*',
        '**/*Test*.*',
        "**/services/**/model/**",
        //Utils
        '**/utils/*.*',
        '**/utils/**/*.*'
]

// Note: **/reviews/ReviewService*.* was added as BazaarVoice cannot be mocked

//To support Java coverage on Unit tests
def debugAppTree = fileTree(dir: "${buildDir}/intermediates/classes/unitTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def debugKotlinAppTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/unitTest", excludes: fileFilter)

def debugSdkTree = fileTree(dir: "..//build/intermediates/classes/unitTest", excludes: fileFilter)
def debugKotlinSdkTree = fileTree(dir: "../submodule/build/tmp/kotlin-classes/unitTest", excludes: fileFilter)


def mainAppSrc = "${project.projectDir}/src/main/java"
def debugAppSrc = "${project.projectDir}/src/debug/java"

def mainSdkSrc = "../submodule/src/main/java"
def debugSdkSrc = "../submodule/src/debug/java"


sourceDirectories = files([mainAppSrc, debugAppSrc,
                           mainSdkSrc, debugSdkSrc])


def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code-coverage/connected/", includes: ["**/*.ec"])
def sdkAndroidTests = fileTree(dir: "../submodule/build/outputs/code-coverage/connected/", includes: ["**/*.ec"])

classDirectories = files([debugAppTree, debugSdkTree,
                          debugKotlinAppTree, debugKotlinSdkTree])
executionData = files("${buildDir}/jacoco/testUnitTestUnitTest.exec"
        , "../submodule/build/jacoco/testUnitTestUnitTest.exec"
        , appAndroidTests
        , sdkAndroidTests
)

}

任何帮助将不胜感激

我使用 palantir 的插件为我的不同模块创建聚合覆盖率报告 - com.palantir.jacoco-full-report

基本上,在您的根 gradle 文件中,您需要将此添加到您的依赖项中:

classpath 'com.palantir:jacoco-coverage:0.4.0'

之后,当您 运行 ./gradlew test jacocoFullReport 时,会在 build/reports/jacoco/jacocoFullReport/ 中创建一份测试报告,评估所有子项目组合产生的覆盖率。

我不确定它是否适用于多种构建类型。

尝试将其合并为单一构建类型并:

应用Gradle文件:

apply plugin: 'jacoco'
android {
   testBuildType "automationTest"
    ...
  buildTypes {
    debug {
        applicationIdSuffix ".debug"
        versionNameSuffix "-debug"
        debuggable true
        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        matchingFallbacks = ['debug']
    }
    // TESTS
    automationTest {
        applicationIdSuffix ".automationTest"
        versionNameSuffix "-automationTest"

        testCoverageEnabled = true
        matchingFallbacks = ['automationTest', 'debug']
    }
    ...

子模块 Gradle 文件:

apply plugin: 'jacoco'
android {

   buildTypes {
    debug {
    }

    automationTest {
        initWith(buildTypes.debug)
        testCoverageEnabled = true
    }

    release {
        initWith(buildTypes.debug)
    }
  ...
}

在子模块中创建单元测试覆盖率的任务:

task createUnitTestReport(type: JacocoReport, dependsOn: ['testAutomationTestUnitTest']) {

reports {
    html.enabled = true
}

def fileFilter = ['**/R.class',
                  '**/BR.class',
                  '**/R$*.class',
                  '**/BR$*.class',
                  '**/BuildConfig.*',
                  '**/*databinding/**/*.*',
                  '**/Manifest*.*',
                  '**/*Test*.*',
                  'android/**/*.*']

//To support Java coverage on Unit tests
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/automationTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/automationTest", excludes: fileFilter)

def mainSrc = "${project.projectDir}/src/main/java"
def debugSrc = "${project.projectDir}/src/debug/java"

sourceDirectories = files([mainSrc, debugSrc])


classDirectories = files([debugTree], [kotlinDebugTree])
executionData = files("${buildDir}/jacoco/testAutomationTestUnitTest.exec")
}

在应用程序中创建全球覆盖的任务:

task createGlobalTestReport(type: JacocoReport,
    dependsOn: [':app:createUiTestReport', ':submodule:createUnitTestReport']) {

reports {
    html.enabled = true
}

def fileFilter = [
        //Android stuff
        '**/R.class',
        '**/BR.class',
        '**/R$*.class',
        '**/BR$*.class',
        '**/BuildConfig.*',
        'android/**/*.*',
        //Data Binding
        '**/*databinding',
        '**/*binders',
        '**/*layouts',
        '**/Manifest*.*',
        '**/*Test*.*'
]

//To support Java coverage on Unit tests
def debugAppTree = fileTree(dir: "${buildDir}/intermediates/classes/automationTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def debugKotlinAppTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/automationTest", excludes: fileFilter)

def debugSdkTree = fileTree(dir: "../submodule/build/intermediates/classes/automationTest", excludes: fileFilter)
def debugKotlinSdkTree = fileTree(dir: "../submodule/build/tmp/kotlin-classes/automationTest", excludes: fileFilter)


def mainAppSrc = "${project.projectDir}/src/main/java"
def debugAppSrc = "${project.projectDir}/src/debug/java"

def mainSdkSrc = "../submodule/src/main/java"
def debugSdkSrc = "../submodule/src/debug/java"


sourceDirectories = files([mainAppSrc, debugAppSrc,
                           mainSdkSrc, debugSdkSrc])

classDirectories = files([debugAppTree, debugSdkTree,
                          debugKotlinAppTree, debugKotlinSdkTree])

def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code-coverage/connected/", includes: ["*.ec"])

executionData = files("${buildDir}/jacoco/testAutomationTestUnitTest.exec"
        , "../submodule/build/jacoco/testAutomationTestUnitTest.exec"
        , appAndroidTests
)
}