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

时间:2017-11-17 15:42:02

标签: android jacoco android-espresso

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

  • App - UI

  • 子模块 - 具有大部分业务逻辑

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

  • 应用程序:UI代码覆盖率(Espresso)

  • 子模块:单元测试代码覆盖率

作为客户的要求,我需要合并这两个报告,以获得应用的整体/全局代码覆盖率

注意:我使用 Gradle版本3.1.2。

App 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']
    }

    ...

SubModule 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,dependsOn:[' 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',                     ':子模块: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
)

}

非常感谢任何帮助

2 个答案:

答案 0 :(得分:2)

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

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

App 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']
    }
    ...

SubModule 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
)
}

答案 1 :(得分:0)

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

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

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

之后,当您在./gradlew test jacocoFullReport中运行build/reports/jacoco/jacocoFullReport/测试报告时,评估所有子项目产生的覆盖范围。