如何从测试范围中排除Dagger2类 [英] How to exclude Dagger2 classes from test coverage
问题描述
是否可以选择从Android Studio的测试覆盖率报告中排除Dagger2类
Is there any option to exclude Dagger2 classes from test coverage report in Android Studio
推荐答案
JaCoCo排除
如果您使用的是JaCoCo,例如使用android工具连接的测试,则需要配置排除项(或包含),根据文档是...
JaCoCo excludes
If you're using JaCoCo, for example using android instrumentation connected tests, you need to configure the excludes (or includes), which, according to the documentation is...
要从报告中排除的类文件列表.可以使用通配符(*和?).如果未指定,则不排除任何内容.
A list of class files to exclude from the report. May use wildcard characters (* and ?). When not specified nothing will be excluded.
这意味着您需要匹配生成的匕首类名称.以下规则实际上涵盖了dagger-compiler
生成的任何类,而不匹配任何未生成的类(除非您将类命名为与dagger相同...):
Which means you need to match the generated dagger class names. The following rules cover virtually any class generated by dagger-compiler
, without matching any of non-generated classes (unless you name your class the same as dagger does...):
excludes = [
'**/*_MembersInjector.class',
'**/Dagger*Component.class', // covers component implementations
'**/Dagger*Component$Builder.class', // covers component builders
'**/*Module_*Factory.class'
]
运行构建后,您可以在app/build/generated/source/apt
目录中检查生成的匕首类,以查看是否有其他要与排除项匹配的生成类.
You can check your generated dagger classes in app/build/generated/source/apt
directory after running a build, to see if there are any additional generated classes that you want to match with excludes.
此排除数组是jacoco插件的配置属性.现在,放置此excludes
数组的位置取决于您是根据jacoco插件定义自己的任务还是使用为您执行此操作的高级插件".例如,使用此插件(您可以查看插件源,以了解排除在何处实际应用):
This excludes array is a configuration property of jacoco plugin. Now, where to put this excludes
array depends on whether you define your own tasks based on the jacoco plugin, or use a 'higher level plugin' that does this for you. For example using this plugin (you can see the plugin source to see where the excludes are actually applied):
jacocoAndroidUnitTestReport {
excludes += [
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class'
]
}
连接的测试
如果您通过在 buildType 中设置testCoverageEnabled true
来运行android已连接的测试覆盖率,那么很遗憾,没有惯用的方法来声明排除,因为 android gradle插件不提供此类选项,并且预定义的
Connected tests
If you're running android connected test coverage by setting testCoverageEnabled true
in your buildType, unfortunately there is no idiomatic way to declare excludes, since the android gradle plugin doesn't provide such options, and the predefined jacoco report task has the excludes hardcoded. In this case, you have to script your own task with excludes.
如果您使用的是IntelliJ测试运行程序,则无论是通过IntelliJ还是JaCoCo进行覆盖,都需要放置 includes 进行测试配置.
If you're using the IntelliJ test runner, whether the coverage is done by IntelliJ or JaCoCo, you need to put the includes for a test configuration.
- 打开编辑配置窗口:
- 选择测试配置并定义包括(类或整个程序包).在这种情况下,我将整个
com.google.android.gms
软件包包括在内,例如:
- Choose your test config and define includes (classes or whole packages). In this case I included the whole
com.google.android.gms
package, just as an example:
要排除匕首生成的文件,最快的方法是将所有匕首依赖项放在一个根包中,并包括所有其他测试配置中的软件包.
To exclude dagger generated files, the quickest way is to put all the dagger dependencies in one root package, and include all the other packages in the test configuration.
这篇关于如何从测试范围中排除Dagger2类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!