当我从Android Studio运行test with coverage
选项时,我得到所有文件的正确覆盖率百分比。
但是,当我使用Jacoco测试报告时,它并没有显示所有文件的正确值。一些文件显示为正确的值,而其他文件显示为0。
这是我的jacoco文件
jacoco.gradle
apply plugin: 'jacoco'
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
ext {
fileFilter = [
'app/account/**',
'app/activity/**',
'app/add_funds/**',
'app/analytics/**',
'app/api/**',
'app/di/**',
'app/common/**',
'app/util/**',
'app/repo/**',
'app/wallet/**',
'app/userprofile/**',
'app/settings/**',
'app/search/**',
'app/referral/**',
'app/portfolio/**',
'app/listing/**',
'app/order/**',
'app/payment/**',
'app/notification/**',
'app/discover/**',
'app/epoxy/**',
'app/home/**',
'app/kyc/**',
'app/user/**',
'app/payment**',
'app/follow/**',
]
}
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 ->
def sourceName
if (!productFlavorName) {
sourceName = "${buildTypeName}"
} else {
sourceName = "${productFlavorName}${buildTypeName.capitalize()}"
}
def testTaskName = "test${sourceName.capitalize()}UnitTest"
def coverageTaskName = "${testTaskName}Coverage"
// Create coverage task of form 'testFlavorTypeUnitTestCoverage' depending on 'testFlavorTypeUnitTest'
task "${coverageTaskName}"(type: JacocoReport, dependsOn: "$testTaskName") {
group = 'Reporting'
description = "Generate Jacoco coverage reports for the ${sourceName.capitalize()} build."
def javaClasses = fileTree(dir: "${project.buildDir}/intermediates/javac/${sourceName}/compile${sourceName.capitalize()}JavaWithJavac/classes", excludes: fileFilter)
def kotlinClasses = fileTree(dir: "${project.buildDir}/tmp/kotlin-classes/${sourceName}", excludes: fileFilter)
def javaSrc = [
'src/main/java',
"src/$productFlavorName/java",
"src/$buildTypeName/java"
]
def kotlinSrc = [
'src/main/kotlin',
"src/$productFlavorName/kotlin",
"src/$buildTypeName/kotlin"
]
sourceDirectories.setFrom(files([javaSrc], [kotlinSrc]))
classDirectories.setFrom(files([javaClasses], [kotlinClasses]))
executionData.setFrom(files("${project.buildDir}/jacoco/${testTaskName}.exec"))
reports {
xml.enabled = true
html.enabled = true
}
}
}
}
}
Buid.gradle是
...
apply from: "jacoco.gradle"
android {
defaultConfig {
debug {
testCoverageEnabled true
}
productFlavors {
staging{}
prod{}
}
testOptions {
/*execution 'ANDROIDX_TEST_ORCHESTRATOR'*/
animationsDisabled true
unitTests {
includeAndroidResources = true
all {
maxHeapSize = "512m"
}
}
}
}
}
我用来生成报告的gradle命令是
./gradlew testStagingDebugUnitTest testStagingDebugUnitTestCoverage