I have an android project with instrument tests.
I am trying to run a custom gradle task immediately after connectedAndroidTest
.
I want this custom task to run irrespective of the connectedAndroidTest
run status i.e. whether it fails or passes.
This custom gradle task is trying to pull the test artefacts from the device after the instrument test runs.
Here is how my task dependency configuration in app's build.gradle looks like:
tasks.register("pullData", Exec) {
// code to pull data
}
gradle.projectEvaluated {
connectedAndroidTest.finalizedBy pullData
}
When the connecteAndroidTest passes, the files are downloaded from device but when it fails the gradle task fails and finalising task 'pullData' is not executed.
My question is: how do I run a task after connectedAndroidTest
irrespective of the test result? Why is finalizedBy not working when my test fails?
Any help with this is much appreciated. Thank you :)
Edit: Solved by following @SimonJacobs suggestion but was not straightforward to access "connectedAndroidTest". This is how I implemented the solution:
tasks.withType(VerificationTask).configureEach {
if (name.startsWith("connected") && name.endsWith("AndroidTest")) {
ignoreFailure = true
}
}
Cheers!
By default a test task will fail the build when any individual test fails, so any subsequent tasks won't run. You need to turn this off to get the behaviour you want.
You can do this for your specific test task as follows:
tasks.named("connectedAndroidTest") {
ignoreFailures = true
}
This has the downside of the build not failing when you have a failing test, which may not be what you want more generally. Addressing this issue would involve further build redesign including not respecting the request in your question to run pullData
after every test run; for these reasons discussion of possible approaches to that are probably outside the scope of this question.