android{
buildTypes {
debug {
testCoverageEnabled = true
}
}
}
该代码允许测试覆盖率,并在调试版本中生成覆盖率结果。
Creating file /Users/me/MyApplication/app/build/outputs/reports/coverage/debug/coverage.xml
Creating file /Users/me/MyApplication/app/build/outputs/reports/coverage/debug/com.vogella.android.testapp.html
Creating directory /Users/me/MyApplication/app/build/outputs/reports/coverage/debug/classes
Creating directory /Users/me/MyApplication/app/build/outputs/reports/coverage/debug/data
Creating directory /Users/me/MyApplication/app/build/coverage/debug
Wrote /Users/me/MyApplication/app/build/outputs/reports/coverage/debug/coverage.xml
其中,coverage.xml文件包含覆盖率结果的详细信息。
打开html文件。在控制台中,找到com.vogella.android.testapp.html文件的路径并在浏览器中打开它。该文件包含图表和覆盖率信息的交互式视图。
使用插件。也可以使用例如JaCoCo或EMMA等插件来生成和查看测试覆盖率结果。在build.gradle文件中添加插件的依赖项,并根据文档进行配置。然后运行测试,在插件的输出文件中查看测试覆盖率结果。例如,使用JaCoCo插件:
dependencies {
...
androidTestImplementation 'org.jacoco:org.jacoco.agent:0.8.5'
}
android {
buildTypes {
debug {
testCoverageEnabled true
...
}
}
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
reports {
xml.enabled true
html.enabled true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc