You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CircleCI 2.0是否支持Android Espresso插桩测试?求config.yml配置帮助

CircleCI 2.0 支持 Android Espresso 插桩测试的配置方案

Absolutely, CircleCI 2.0 does support Android Espresso instrumentation tests! It’s super common to hit snags getting instrumentation tests running even when unit tests work smoothly, so let’s walk through a solid config.yml setup that should fix your issue.

完整的 Config.yml 示例

version: 2.1

orbs:
  android: circleci/android@2.3.0

jobs:
  build-and-test:
    executor: android/android-machine
    steps:
      - checkout

      # 缓存依赖,大幅加快后续构建速度
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}

      - run:
          name: 下载项目依赖
          command: ./gradlew androidDependencies

      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}

      # 运行你已经能正常执行的本地单元测试
      - run:
          name: 执行单元测试
          command: ./gradlew testDebugUnitTest

      # 核心步骤:配置并启动 Android 模拟器
      - run:
          name: 创建并启动测试模拟器
          command: |
            # 下载适配的系统镜像(可根据你的 targetSdkVersion 调整版本)
            sdkmanager "system-images;android-30;google_apis;x86"
            # 创建模拟器实例
            echo "no" | avdmanager create avd -n test_emulator -k "system-images;android-30;google_apis;x86" --force
            # 后台启动模拟器,适配CI无界面环境
            $ANDROID_HOME/emulator/emulator -avd test_emulator -no-window -no-audio -no-boot-anim -gpu swiftshader_indirect &

      # 必须等待模拟器完全启动就绪再执行测试
      - run:
          name: 等待模拟器启动完成
          command: |
            circle-android wait-for-boot
            # 解锁模拟器屏幕(避免测试因锁屏失败)
            adb shell input keyevent 82 &

      # 运行 Espresso 插桩测试
      - run:
          name: 执行 Espresso 插桩测试
          command: ./gradlew connectedDebugAndroidTest

      # 可选:保存测试报告,方便排查失败原因
      - store_artifacts:
          path: app/build/reports
          destination: test-reports

      - store_test_results:
          path: app/build/test-results

关键配置说明

  • 官方 Android Orb:使用 android/android-machine 执行器,它预安装了大部分 Android 开发工具,省去了手动配置 SDK 的繁琐步骤。
  • 模拟器优化:
    • 选择 x86 架构的系统镜像,启动速度远快于 armeabi 镜像;版本可根据你的 app 适配需求调整。
    • 启动参数 -no-window -no-audio -no-boot-anim 适配 CI 的无界面运行环境,-gpu swiftshader_indirect 能提升模拟器渲染性能,避免卡顿。
  • 模拟器就绪检测:circle-android wait-for-boot 是 CircleCI 专门提供的工具,能确保模拟器完全启动、系统加载完成后再执行测试——这是很多人忽略的核心细节,直接跑测试大概率会失败。
  • 插桩测试命令:connectedDebugAndroidTest 是 Gradle 专门用于设备/模拟器上插桩测试的任务,和本地单元测试的 testDebugUnitTest 是完全独立的任务。

常见问题排查

如果还是失败,可以检查这些点:

  • 确认你的 AndroidManifest.xml 中添加了 Espresso 测试所需的权限(比如测试涉及网络时的 android.permission.INTERNET)。
  • 核对模拟器系统镜像的版本是否和你的 app 的 minSdkVersion 兼容。
  • 查看存储的测试报告,错误日志通常能直接定位是模拟器启动问题,还是测试代码本身的逻辑问题。

内容的提问来源于stack exchange,提问作者president

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:18:02