我正在尝试生成 Jacoco 代码覆盖率报告。我已将 AndroidTestCase 用于我的测试类。
我发现使用 testCoverageEnabled true 并使用默认 android -studio default jacoco、./gradlew connectedCheck 或 createDebugCoverageReport 创建成功/失败测试用例的百分比,但没有覆盖率报告。
然后我尝试了 jacoco {toolVersion "0.7.1.201405082137"} 和任务 jacocoTestReport(type:JacocoReport, dependsOn: "testDebug")。我试图通过各种任务更改dependsOn 值。该报告显示 0(零)测试覆盖率,这是不可能的,因为所有类中至少有一半经过测试。
在过去的几天里,我遵循了各种公认的堆栈溢出答案。结果是否定的。
我的毕业文件:
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "test.gradle.com.myapplicationtestgradle"
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(
'proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
jacoco {
version "0.7.1.201405082137"
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
jacoco {
toolVersion "0.7.1.201405082137"
}
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
// exclude auto-generated classes and tests
def fileFilter = ['**/R.class', '**/R$*.class',
'**/BuildConfig.*', '**/Manifest*.*',
'android/**/*.*']
def debugTree = fileTree(dir:
"${project.buildDir}/intermediates/classes/debug",
excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
additionalSourceDirs = files([
"${buildDir}/generated/source/buildConfig/debug",
"${buildDir}/generated/source/r/debug"
])
executionData = fileTree(dir: project.projectDir, includes:
['**/*.exec', '**/*.ec'])
reports {
xml.enabled = true
xml.destination = "${buildDir}/jacocoTestReport.xml"
csv.enabled = false
html.enabled = true
html.destination = "${buildDir}/reports/jacoco"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
}