Issue
I have an instrumented test class. Its test cases are in JUnit 5 (with the help of a Gradle plugin per this answer). How can I disable this test class from being run on CI (specifically, on GitHub Actions)?
I cannot use JUnit 5 annotations like @DisabledIfEnvironmentVariable
because Android instrumented tests run on the device/emulator and cannot see the Host OS environment variables.
Solution
All GitHub workflows have access to an environment variable CI
which is always true
(Docs).
With that in mind, we can disable our test class indirectly using Gradle.
In order to do that, declare a buildConfig field like this in your app/build.gradle[.kts]:
android {
buildTypes {
getByName("debug") {
val isCI = System.getenv("CI")?.toBoolean() ?: false
buildConfigField("Boolean", "CI", "$isCI")
}
// ...
Disable the test class/method with @DisabledIfBuildConfigValue
provided by the plugin like so:
@DisabledIfBuildConfigValue(named = "CI", matches = "true")
class ScreenshotTest {
// ...
Executing the tests with Gradle, the above classes/methods will be skipped when run on GitHub:
./gradlew :app:connectedAndroidTest --stacktrace
Answered By - Mahozad