如何在 GitHub 操作上禁用某些 JUnit 5 Android 检测测试?
How to disable some JUnit 5 Android instrumented tests on GitHub Actions?
我有一个仪器测试class。它的测试用例在 JUnit 5 中(借助每个 this answer 的 Gradle 插件)。我如何禁用此测试 class 在 CI 上 运行(具体来说,在 GitHub 操作上)?
我不能像 @DisabledIfEnvironmentVariable
这样使用 JUnit 5 注释,因为 Android 在 device/emulator 上检测了测试 运行 并且看不到主机 OS 环境变量。
所有 GitHub 工作流都可以访问环境变量 CI
,该变量始终是 true
(Docs)。
考虑到这一点,我们可以使用 Gradle.
间接禁用我们的测试 class
为此,请在 app/build.gradle[.kts]:
中声明这样的 buildConfig 字段
android {
buildTypes {
getByName("debug") {
val isCI = System.getenv("CI")?.toBoolean() ?: false
buildConfigField("Boolean", "CI", "$isCI")
}
// ...
使用 the plugin 提供的 @DisabledIfBuildConfigValue
禁用测试 class/method,如下所示:
@DisabledIfBuildConfigValue(named = "CI", matches = "true")
class ScreenshotTest {
// ...
使用 Gradle 执行测试,当 运行 on GitHub 时将跳过上述 classes/methods:
./gradlew :app:connectedAndroidTest --stacktrace
我有一个仪器测试class。它的测试用例在 JUnit 5 中(借助每个 this answer 的 Gradle 插件)。我如何禁用此测试 class 在 CI 上 运行(具体来说,在 GitHub 操作上)?
我不能像 @DisabledIfEnvironmentVariable
这样使用 JUnit 5 注释,因为 Android 在 device/emulator 上检测了测试 运行 并且看不到主机 OS 环境变量。
所有 GitHub 工作流都可以访问环境变量 CI
,该变量始终是 true
(Docs)。
考虑到这一点,我们可以使用 Gradle.
间接禁用我们的测试 class
为此,请在 app/build.gradle[.kts]:
android {
buildTypes {
getByName("debug") {
val isCI = System.getenv("CI")?.toBoolean() ?: false
buildConfigField("Boolean", "CI", "$isCI")
}
// ...
使用 the plugin 提供的 @DisabledIfBuildConfigValue
禁用测试 class/method,如下所示:
@DisabledIfBuildConfigValue(named = "CI", matches = "true")
class ScreenshotTest {
// ...
使用 Gradle 执行测试,当 运行 on GitHub 时将跳过上述 classes/methods:
./gradlew :app:connectedAndroidTest --stacktrace