I have a specific build variant that is ONLY used for mock testing. I'd prefer not to run unit tests against this variant (but want to run them against other variants). Is there any way to inform gradle to skip unit testing for this specific variant?
-
Did you manage to solve it? – Daimon Apr 02 '15 at 14:16
-
I didn't manage to solve it, which is unfortunate. We had close to 4 different variants and with 1000+ unit (robolectric) tests our builds would take upwards of 16+ mins to complete for all variants combined. – Eric Miles Apr 04 '15 at 01:02
4 Answers
In my case I wanted to skip all unit testing from the release variant. It can be achieved doing this:
gradle.taskGraph.useFilter { task ->
if (task.name.startsWith("test") && task.name.contains("ReleaseUnitTest")) {
return false;
}
return true;
}

- 1,079
- 14
- 20
-
expressed in a slightly different (and sweeter?) way: gradle.taskGraph.useFilter { task -> !( task.name.startsWith("test") && task.name.contains("ReleaseUnitTest") ) } – Achraf Amil May 16 '18 at 15:41
-
what version of gradle was this? I don't see it in docs and get error `No signature of method: org.gradle.execution.taskgraph.DefaultTaskExecutionGraph.useFilter() is applicable for argument types: (build_ebr7m9zqmz5ikthuvqpf4170d$_run_closure4) values: [build_ebr7m9zqmz5ikthuvqpf4170d$_run_closure4@4a670d2d]` – arekolek Sep 15 '22 at 13:26
-
Ok, now I see this is not a public API https://stackoverflow.com/a/58869648/1916449 – arekolek Sep 15 '22 at 13:28
I wanted to skip tests for specific release. I got it worked using the below.
tasks.whenTaskAdded {task ->
if(task.name.contains("task")) {
task.enabled = false
}
}
If you have multiple tasks to be disabled ,
tasks.whenTaskAdded {task ->
if(task.name.contains("task1") || task.name.contains("task2") || task.name.contains("task3")) {
task.enabled = false
}
}

- 361
- 4
- 5
Gradle lets you call only the tasks that you specifically want executed.
If executing Gradle tasks directly, instead of calling 'test' you could call 'testReleaseVariant' to only execute tests on ReleaseVariant. You can also string together other non mock variants: 'testReleaseVariant testFreeVariant' etc.
If you are calling the tasks through Run/Debug menu in Android Studio you can edit the configuration by clicking on the dropdown next to the green play button and selecting "edit configurations". Under Gradle->Tests in 'app' and in the "Tasks" field you can specify "testReleaseVariant", or string together any other tasks you want executed.

- 26
- 2
You can exclude tasks from being executed by adding a filter to the taskGraph
:
gradle.taskGraph.useFilter { task ->
task.name != "testMock"
}
This will skip the task, but other than e.g. task.enabled = false
this will also skip its dependencies, so compileJava
et al. will not be executed for the mock variant.

- 1,560
- 2
- 13
- 20