如何在 GitHub Actions 上禁用一些 JUnit 5 Android 检测测试? [英] How to disable some JUnit 5 Android instrumented tests on GitHub Actions?

查看:34
本文介绍了如何在 GitHub Actions 上禁用一些 JUnit 5 Android 检测测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仪器测试类.它的测试用例在 JUnit 5 中(在 this answer 的 Gradle 插件的帮助下).如何禁止在 CI 上运行此测试类(特别是在 GitHub Actions 上)?

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)?

我不能使用像 @DisabledIfEnvironmentVariable 这样的 JUnit 5 注释,因为 Android 检测测试在设备/模拟器上运行并且看不到主机操作系统环境变量.

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.

推荐答案

所有 GitHub 工作流都可以访问环境变量 CI,该变量始终为 true (文档).

All GitHub workflows have access to an environment variable CI which is always true (Docs).

考虑到这一点,我们可以使用 Gradle 间接禁用我们的测试类.
为此,请在您的 app/build.gradle[.kts] 中声明一个像这样的 buildConfig 字段:

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")
    }
// ...

使用插件提供的@DisabledIfBuildConfigValue禁用测试类/方法 像这样:

Disable the test class/method with @DisabledIfBuildConfigValue provided by the plugin like so:

@DisabledIfBuildConfigValue(named = "CI", matches = "true")
class ScreenshotTest {
// ...

使用 Gradle 执行测试,在 GitHub 上运行时将跳过上述类/方法:

Executing the tests with Gradle, the above classes/methods will be skipped when run on GitHub:

./gradlew :app:connectedAndroidTest --stacktrace

这篇关于如何在 GitHub Actions 上禁用一些 JUnit 5 Android 检测测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆