Filip
05/16/2025, 2:21 PMtest, most android modules have testDebugUnitTest and the modules with flavors have also the flavor name inside the task name, like testStagingDebugUnitTest .
Our goal however is be able to run all existing unit tests in a CI environment using a single command, e.g. runAllTest.
Additionally, since there are different source sets in some of the flavor android modules, we'd like to have an option to configure what tasks in a given module should be executed to cover all source sets.
What would be the recommended way of approaching this topic, that would follow the best practices and work properly with configuration avoidance and configuration cache?
In the thread I'm pasting an initial draft of the solution, which definitely can be improved (it uses afterEvaluate, which I'm aware is not recommended).
There's a convention plugin to register runAllTest task, making it dependent on the selected test task name and a custom extension to allow a module define its desired task name.Filip
05/16/2025, 2:22 PMclass RunAllTestsPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
val testVariantsExtension = extensions.create(
ConfigureTestTaskExtension.EXTENSION_NAME,
ConfigureTestTaskExtension::class,
)
afterEvaluate {
val testTasks = testVariantsExtension.customTestTasksNames
.ifEmpty { listOf(testVariantsExtension.defaultTestTaskName) }
.map(tasks::named)
tasks.register("runAllTests") {
dependsOn(testTasks)
}
}
}
}
abstract class ConfigureTestTaskExtension {
val defaultTestTaskName = "testDebugUnitTest"
val customTestTasksNames = mutableListOf<String>()
companion object {
const val EXTENSION_NAME = "configureTestTask"
}
}
fun Project.configureTestTask(action: ConfigureTestTaskExtension.() -> Unit) {
extensions.configure<ConfigureTestTaskExtension>(action)
}
and then in the build.gradle for modules with special needs:
configureTestTask {
customTestTasksNames += listOf("testStagingDebugUnitTest", "testLiveDebugUnitTest")
}ephemient
05/16/2025, 2:34 PMandroidComponents.beforeVariants to control which unit tests will run, then ./gradlew test will run them allFilip
05/16/2025, 2:42 PMVampire
05/16/2025, 2:42 PMcheck which runs tests and maybe also further verification tasks and each project can decide which tasks check depends on.Vampire
05/16/2025, 2:44 PMephemient
05/16/2025, 2:45 PMAndrzej Zabost
05/16/2025, 2:48 PMenableUnitTest is that it will disable some tests completely, so they won't execute even if I wanted to, by running a specific variant's unit tests task explicitlyFilip
05/16/2025, 3:04 PMLibraryVariantBuilder implements both those interfaces and both of them have the same name for this property.
By default, the one from VariantBuilder is referenced and that's why the warning appears.