【问题标题】:How to disable some JUnit 5 Android instrumented tests on GitHub Actions?如何在 GitHub Actions 上禁用一些 JUnit 5 Android 检测测试?
【发布时间】:2021-08-22 15:19:52
【问题描述】:

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

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

【问题讨论】:

    标签: junit github-actions junit5 android-testing


    【解决方案1】:

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

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

    android {
      buildTypes {
        getByName("debug") {
          val isCI = System.getenv("CI")?.toBoolean() ?: false
          buildConfigField("Boolean", "CI", "$isCI")
        }
    // ...
    

    使用the plugin 提供的@DisabledIfBuildConfigValue 禁用测试类/方法,如下所示:

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

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

    ./gradlew :app:connectedAndroidTest --stacktrace
    

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2020-11-21
      • 1970-01-01
      • 2018-02-20
      • 2021-04-16
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多