我用“gradlew createDebugCoverageReport”运行我的测试。我的问题是覆盖率报告包含我拥有的每个源文件。我想排除一些文件。我将它添加到我的build.gradle中,但它不起作用:
sourceSets {
androidTest {
java
{
exclude '**/TouchImageView.java'
}
}
}
答案 0 :(得分:3)
你必须在build.gradle的开头添加jacoco插件
apply plugin: 'jacoco'
然后使用testCoverageEnabled true
启用覆盖范围,即
buildTypes {
release {
...
}
debug {
testCoverageEnabled true
}
}
创建任务jacocoTestReport
:
task jacocoTestReport(type:JacocoReport, dependsOn: "connectedDebugAndroidTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
// exclude auto-generated classes and tests
def fileFilter = ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*IScript*.*',
'android/**/*.*',
'**/*_Factory*',
'**/*_MembersInjector*',
'**/*Fake*']
def debugTree = fileTree(dir:
"${project.buildDir}/intermediates/classes/debug",
excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
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"
}
}
将您的排除项添加到fileFilter
数组中。
然后运行报告:
$ gradle jacocoTestReport