开发者设备平台设备运行概览

欢迎使用 Developer Device Platform Device Run,您可以在云端对实体和虚拟 Android 设备以及实体 iOS 设备运行测试。本页介绍了运行 Android 和 iOS 测试的共同点。如需了解特定于平台的步骤,请参阅 Android 的设备运行和 iOS 的设备运行。

如需使用此 Google Cloud CLI,您需要提供 Google Cloud 项目 ID。如需查看命令摘要,请参阅:gcloud beta device-run

请注意,以下标志是必需的:

  • 设备:使用 --device 指定设备:--device shiba-35 或 --device iphonese3-18-4
  • 测试:使用 --test 指定测试 APK 或 IPA:--test /path/to/test.apk 或 --test /path/to/test.ipa

以下配置选项适用于 Android 和 iOS 测试。

输入和输出 Cloud Storage 路径

  • 自定义 Cloud Storage 存储桶:您可以为输入和输出指定自定义 Cloud Storage 存储桶:--bucket-name=my-custom-bucket 如果您未使用 --bucket-name= 标志指定 Cloud Storage 存储桶,Google Cloud CLI 将使用名为 PROJECT_ID-devicerun 的默认存储桶。
  • 输入文件:当您使用 --test、--additional-apps 或 --xctestrun-file 标志指定本地路径时,Google Cloud CLI 会在每次执行命令时自动将其复制到 Cloud Storage 存储桶中的 gs://BUCKET_NAME/automation/inputs/DATE_TIME_FOUR_CHARS_SUFFIX/ 下,其中 BUCKET_NAME 默认为 PROJECT_ID-devicerun。
  • 云路径:由于上传大型文件可能非常耗时,因此您可以直接使用其 Cloud Storage gs:// 路径来引用这些文件,从而节省上传时间。
  • 结果文件:输出制品会上传到 Cloud Storage 存储桶中的 gs://BUCKET_NAME/automation/sessions/session-[SESSION_ID]/ 下。

同步运行和异步运行

默认情况下,提交命令是阻塞(同步)的。它将等待测试完成,并输出结果的摘要表格。

如需提交会话并在将文件上传到 Cloud Storage 后立即返回,请添加 --async 标志:

Android

gcloud beta device-run sessions submit instrumentation \
  --device shiba-35 \
  --test ./ANDROID_TESTS.apk \
  --async

iOS

gcloud beta device-run sessions submit xctest \
  --device iphonese3-18-4 \
  --test ./IOS_TESTS.zip \
  --async

此方法会立即返回会话 ID。然后,您可以使用 sessions wait 命令等待该操作完成:

gcloud beta device-run sessions wait SESSION_ID

该命令会一直阻塞,直到作业完成。

多设备

如需在多台设备上运行相同的测试,请提供 --device 标志,其中包含多个以英文逗号分隔的设备 ID,或者提供多个 --device 标志,每个标志指定一个不同的设备 ID。

Android

gcloud beta device-run sessions submit instrumentation \
  --device shiba-34,tokay-36 \
  --test ./ANDROID_TESTS.apk

或:

gcloud beta device-run sessions submit instrumentation \
  --device shiba-34 \
  --device tokay-36 \
  --test ./ANDROID_TESTS.apk

iOS

gcloud beta device-run sessions submit xctest \
  --device iphonese3-18-4,iphonese3-26-3 \
  --test ./IOS_TESTS.zip

或:

gcloud beta device-run sessions submit xctest \
  --device iphonese3-18-4 \
  --device iphonese3-26-3 \
  --test ./IOS_TESTS.zip

测试超时

限制测试的执行时长:

Android

gcloud beta device-run sessions submit instrumentation \
  --device shiba-35 \
  --test ./ANDROID_TESTS.apk \
  --instrumentation-timeout=10m

iOS

gcloud beta device-run sessions submit xctest \
  --device iphonese3-18-4 \
  --test ./IOS_TESTS.zip \
  --xctest-timeout=10m

有效范围为 1m 到 1h,默认值为 5m。

软件版本

在配置测试运行之前,发现并使用 AndroidX Test Orchestrator 和 Apple Xcode 的可用软件包和版本,并检查它们的生命周期、支持的版本和元数据。

首先,列出所有可用的软件包及其版本:

gcloud beta device-run software-versions list

收到类似如下的输出:

ID                                NAME                              SOFTWARE_TYPE               VERSION  STATE   IS_DEFAULT
androidx-test-orchestrator-1-4-1  AndroidX Test Orchestrator 1.4.1  ANDROIDX_TEST_ORCHESTRATOR  1.4.1    ACTIVE  True
androidx-test-orchestrator-1-6-1  AndroidX Test Orchestrator 1.6.1  ANDROIDX_TEST_ORCHESTRATOR  1.6.1    ACTIVE
xcode-16-4                        Xcode 16.4                        XCODE                       16.4     ACTIVE
xcode-26-2                        Xcode 26.2                        XCODE                       26.2     ACTIVE  True

然后,使用列表中的确切资源 ID 描述特定软件包:

Android

gcloud beta device-run software-versions describe androidx-test-orchestrator-1-4-1

iOS

gcloud beta device-run software-versions describe xcode-16-4

收到类似如下的输出:

Android

id: androidx-test-orchestrator-1-4-1
isDefault: true
lifecycle:
  state: ACTIVE
name: AndroidX Test Orchestrator 1.4.1
softwareType: ANDROIDX_TEST_ORCHESTRATOR
version: 1.4.1

iOS

id: xcode-16-4
lifecycle:
  state: ACTIVE
name: Xcode 16.4
softwareType: XCODE
supportedIosVersions:

-   '15.0'
-   '15.1'
-   '15.2'
-   '15.4'
-   '15.7'
-   '16.0'
-   '16.1'
-   '16.2'
-   '16.3'
-   '16.5'
-   '16.6'
-   '18.0'
-   '18.3'
-   '18.4'
version: '16.4'

现在,您已经知道哪些软件版本可用,可以使用 --orchestrator-version 或 --xcode-version 标志在测试运行中使用这些版本,如下所示:

Android

gcloud beta device-run sessions submit instrumentation \
  --device shiba-35 \
  --test ./ANDROID_TESTS.apk \
  --orchestrator-version=1.4.1

如需了解详情,请参阅启用 Orchestrator。

iOS

gcloud beta device-run sessions submit xctest \
  --device iphonese3-18-4 \
  --test ./IOS_TESTS.zip \
  --xcode-version=16.4

如需了解详情,请参阅 Device Run for iOS。

输入验证

Developer Device Platform 会尝试验证您的输入内容(包括制品),并在遇到错误输入内容时返回 ERROR,而不是 PASSED 或 FAILED:

Session [session-94b57778] finished with result [ERROR].
JOB NAME  EXECUTION NAME  EXECUTION RESULT
job-000   execution-000   ERROR

现在,将 --full 附加到 sessions describe 命令,以查看有关会话的更多详细信息:

gcloud beta device-run sessions describe --full session-94b57778

这会生成类似如下的输出:

name: projects/user-test1/locations/global/sessions/session-94b57778
sessionConfig:
  displayName: instrumentation-session
  jobConfigs:
  -   action:
      androidInstrumentationTest:
        testInstallable:
          files:
          -   gcsInputFile:
              path: gs://user-test1-devicerun/automation/inputs/2026-09-16_18:55.17372947_EECF/my-test.apk
    allocationConfig:
      deviceConfigs:
      -   actions:
        -   androidLogcat: {}
        requirement:
          deviceId: mediumphone-arm-34
    displayName: job-000
  outputDirectoryConfig:
    gcsOutputDirectory:
      path: gs://user-test1-devicerun/automation/sessions
sessionReport:
  endTime: '2026-09-16T18:55:49.721Z'
  id: ed681f23-669b-4464-8a93-b28456e58136
  jobReports:
  -   displayName: job-000
    endTime: '2026-09-16T18:55:45.839Z'
    executionReports:
    -   displayName: execution-000
      endTime: '2026-09-16T18:55:46.694Z'
      id: ce370173-af94-4dd7-acc6-0863e4d2e9ec
      result:
        cause:
          summary:
            message: The instrumentation target package does not match any of the
              installable packages.
            reason: INSTRUMENTATION_TARGET_MISMATCH
            type: CUSTOMER
        resultType: ERROR
      startTime: '1970-01-01T00:00:00Z'
      status:
        statusType: PENDING
    id: 953b5106-3c20-40f0-aceb-18af89cd1b83
    result:
      cause:
        summary:
          message: The instrumentation target package does not match any of the installable
            packages.
          reason: INSTRUMENTATION_TARGET_MISMATCH
          type: CUSTOMER
      resultType: ERROR
    startTime: '2026-09-16T18:55:31.651Z'
    status:
      statusType: DONE
  result:
    resultType: ERROR
  startTime: '2026-09-16T18:55:24.442Z'
  status:
    statusType: DONE

后续步骤

使用开发者设备平台运行 Android 测试或运行 iOS 测试。