Jacoco只覆盖androidTest目录而不是在Android Studio中测试。我该怎么办?

时间:2016-08-24 10:07:45

标签: android junit jacoco

我有一个问题,我的jacoco可以覆盖androidTest目录而不是测试目录。我怎么能让jacoco也覆盖测试目录。

这是我的傻瓜

apply plugin: "jacoco"
buildTypes {
    debug{
        testCoverageEnabled true
    }
}
jacoco {
toolVersion = "0.7.6.201602180812"
reportsDir = file("$buildDir/customJacocoReportDir")}

当我运行gradlew createDebugCoverageReport时。有效。但只有AndroidTest覆盖。没有。在Android Studio的项目中,只覆盖了androidTest目录,测试目录未涵盖。

2 个答案:

答案 0 :(得分:0)

这是我的jacoco设置,它不仅涵盖了androidTest目录,而且还能够使用不同的风格。

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.7.7.201606060606"
}

project.afterEvaluate {
// Grab all build types and product flavors
def buildTypes = android.buildTypes.collect { type ->
    type.name
}
def productFlavors = android.productFlavors.collect { flavor ->
    flavor.name
}
// When no product flavors defined, use empty
if (!productFlavors) productFlavors.add('')
productFlavors.each { productFlavorName ->
    buildTypes.each { buildTypeName ->

        // If buildtype and flavor combo is not available, skip
        if (ignoreBuildFlavor(productFlavorName, buildTypeName)) {
            return
        }

        def sourceName, sourcePath
        if (!productFlavorName) {
            sourceName = sourcePath = "${buildTypeName}"
        } else {
            sourceName = "${productFlavorName}${buildTypeName.capitalize()}"
            sourcePath = "${productFlavorName}/${buildTypeName}"
        }
        def testTaskName = "test${sourceName.capitalize()}UnitTest"
        // Create coverage task of form 'testFlavorTypeCoverage' depending on 'testFlavorTypeUnitTest'
        task "${testTaskName}Coverage"(type: JacocoReport, dependsOn: "$testTaskName") {
            group = "Reporting"
            description = "Generate Jacoco coverage reports on the ${sourceName.capitalize()} build."
            classDirectories = fileTree(
                    dir: "${project.buildDir}/intermediates/classes/${sourcePath}",
                    excludes: ['**/R.class',
                               '**/R$*.class',
                               '**/*$ViewInjector*.*',
                               '**/*$ViewBinder*.*',
                               '**/BuildConfig.*',
                               '**/Manifest*.*',
                               '**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name.
                               '**/*$inlined$*.*', // Kotlin specific, Jacoco can not handle several "$" in class name.
                               '**/*Module.*', // Modules for Dagger.
                               '**/*Dagger*.*', // Dagger auto-generated code.
                               '**/*MembersInjector*.*', // Dagger auto-generated code.
                               '**/*_Provide*Factory*.*',// Dagger auto-generated code.
                               '**/*$Icepick*.*']
            )
            def coverageSourceDirs = [
                    "src/main/java",
                    "src/$productFlavorName/java",
                    "src/$buildTypeName/java"
            ]
            additionalSourceDirs = files(coverageSourceDirs)
            sourceDirectories = files(coverageSourceDirs)
            executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
            reports {
                xml.enabled = true
                html.enabled = true
            }
        }
    }
}

不要忘记将此行添加到build.gradle:

apply from: "jacoco.gradle"

答案 1 :(得分:0)

最后,我使用了android-junit-jacoco-plugin。这个插件解决了我的问题。但是如果你使用这个插件,单元测试必须全部成功。如果没有,覆盖率报告将不会由jacoco创建。