JaCoCo 排除
如果您使用 JaCoCo,例如使用 android 仪器连接测试,您需要配置excludes(或包括),根据文档 http://www.eclemma.org/jacoco/trunk/doc/report-mojo.html#excludes is...
要从报告中排除的类文件的列表。可以使用通配符(* 和 ?)。如果未指定,则不会排除任何内容。
这意味着您需要匹配生成的匕首类名称。以下规则几乎涵盖了由dagger-compiler
,不匹配任何非生成的类(除非您将类命名为与 dagger 相同...):
excludes = [
'**/*_MembersInjector.class',
'**/Dagger*Component.class', // covers component implementations
'**/Dagger*Component$Builder.class', // covers component builders
'**/*Module_*Factory.class'
]
您可以在中检查生成的匕首类app/build/generated/source/apt
运行构建后的目录,以查看是否有任何您想要与排除匹配的其他生成的类。
This excludesarray 是 jacoco 插件的配置属性。现在,把这个放在哪里excludes
array 取决于您是基于 jacoco 插件定义自己的任务,还是使用为您执行此操作的“更高级别的插件”。例如使用这个插件 https://github.com/arturdm/jacoco-android-gradle-plugin(您可以查看插件源以查看排除的实际应用位置):
jacocoAndroidUnitTestReport {
excludes += [
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class'
]
}
连接测试
如果您通过设置运行 android 连接测试覆盖率testCoverageEnabled true
在你的构建类型,不幸的是没有惯用的方式来声明excludes,自从Android Gradle 插件 https://google.github.io/android-gradle-dsl/current/不提供这样的选项,并且预定义的jacoco报告任务 https://android.googlesource.com/platform/tools/base/+/03842a2bd83ffcee6c1b8649ae6dbe038e29bf2c/build-system/gradle/src/main/groovy/com/android/build/gradle/internal/coverage/JacocoReportTask.groovy已排除硬编码。在这种情况下,您必须编写自己的任务脚本excludes.
IntelliJ 测试运行器
如果您使用 IntelliJ 测试运行程序,无论覆盖率是由 IntelliJ 还是 JaCoCo 完成,您都需要将includes用于测试配置。
- 打开编辑配置 window:
- 选择您的测试配置并定义包含(类或整个包)。在这种情况下,我包括了整个
com.google.android.gms
包,仅作为示例:
To excludedagger 生成的文件,最快的方法是将所有 dagger 依赖项放在一个根包中,并且include一切other测试配置中的包。