如何从测试覆盖率中排除 Dagger2 类 [英] How to exclude Dagger2 classes from test coverage

查看:24
本文介绍了如何从测试覆盖率中排除 Dagger2 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何选项可以从 Android Studio 的测试覆盖率报告中排除 Dagger2 类

解决方案

JaCoCo 排除

如果您使用 JaCoCo,例如使用 android 检测连接测试,您需要配置 排除(或包含),根据

  1. 选择您的测试配置并定义包含(类或整个包).在本例中,我包含了整个 com.google.android.gms 包,仅作为示例:

排除 dagger 生成的文件,最快的方法是将所有 dagger 依赖项放在一个根包中,并包含所有其他测试配置中的包.

Is there any option to exclude Dagger2 classes from test coverage report in Android Studio

解决方案

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.

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'
]

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.

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'
    ]
}

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 test runner

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.

  1. Open the Edit Configurations window:

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆