Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude Dagger2 classes from test coverage

Tags:

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

like image 673
Mladen Rakonjac Avatar asked Mar 03 '17 10:03

Mladen Rakonjac


People also ask

How do you exclude a class from code coverage?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

How do you exclude classes in JaCoCo code coverage?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.


1 Answers

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:

Edit Configurations

  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:

Test coverage includes

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.

like image 131
maciekjanusz Avatar answered Sep 19 '22 18:09

maciekjanusz