> [!WARNING]
>
> **Preview**
>
>
> This product or feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section
> of the [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA products and features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

This guide describes how to run an iOS XCTest using the [`gcloud alpha
device-run`](https://docs.cloud.google.com/sdk/gcloud/reference/alpha/device-run/sessions/submit/xctest) CLI
and find your results in the Google Cloud console. It assumes you have a
Google Cloud account and project.

If you are migrating from [Firebase Test
Lab](https://firebase.google.com/docs/test-lab) to Developer Device Platform, see our
[migration guide](https://docs.cloud.google.com/developer-device-platform/migrate.md.txt), [command and flag
translation](https://docs.cloud.google.com/developer-device-platform/migrate/flag-command-translation), and
[migration skill](https://docs.cloud.google.com/developer-device-platform/migrate/migration-skill.md.txt) for AI
agents.

To use the `device-run` CLI, you will need to provide your Google Cloud
project ID. See [`gcloud alpha
device-run`](https://docs.cloud.google.com/sdk/gcloud/reference/alpha/device-run) for a summary of commands.

## Before you begin

These steps assume that you have already:

1. Created a Google Cloud project.
2. Set up Developer Device Platform following the [Quickstart](https://docs.cloud.google.com/developer-device-platform/quickstart.md.txt).
3. Authenticated with `gcloud` in the terminal.
4. Reviewed the [Device Run overview](https://docs.cloud.google.com/developer-device-platform/device-run.md.txt) for general information.
5. Compiled your tests and [packaged them into a ZIP](https://docs.cloud.google.com/developer-device-platform/device-run/ios.md.txt#prepare-package).

## Step 1. Configure your test run

> [!TIP]
> **Tip:** To view all available options for the `submit xctest` command, run `gcloud
> alpha device-run sessions submit xctest --help`.

You can customize your test execution using the following optional flags
(although at least one instance of the `--device` flag is required):

- *Multiple devices* : Specify the `--device` flag multiple times to run the same test on multiple devices concurrently: `--device iphonese3-18-4
  --device iphonese3-26-3` or `--device iphonese3-18-4,iphonese3-26-3`
- *Additional apps* : Install one or more additional `.ipa` packages before launching the test using `--additional-apps`: `--additional-apps=/path/to/AdditionalApp.ipa`
- *Custom `.xctestrun` file* : Pass a custom configuration file with `--xctestrun-file`: `--xctestrun-file=/path/to/CustomTests.xctestrun`
- [*Apple Xcode versions*](https://docs.cloud.google.com/developer-device-platform/device-run/ios.md.txt#software-versions): Directly access by CLI supported Xcode versions with the `--xcode-version` flag, for example: `--xcode-version=xcode-16-4`
- *Test timeout* : Limit the execution duration: `--xctest-timeout=10m` (Valid range is `1m` to `1h` and defaults to `5m`).
- *Labels* : Attach user-defined metadata to the session: `--labels=env=staging,owner=my-team`
- *Video recording* : Record one video for each UI test case: `--video=always`. To keep only the videos of failed test cases, use `--video=on-failure`. Video recording requires iOS 17 or later, and unit tests aren't recorded.
- *Push files to device* : Push files to the app data container before test execution: `--other-files-to-push=/tmp/data.txt=com.example.app:/Documents/data.txt`
- *Pull files from device* : Pull files from the app data container after test execution: `--paths-to-pull=com.example.app:/Documents/output.txt`
- *Flaky test retries* : Set the maximum number of attempts to rerun flaky tests: `--flaky-test-attempts=3` (Defaults to 1 attempt).
- *Custom Cloud Storage bucket* : Specify an existing Cloud Storage bucket name (without a `gs://` scheme or path) for inputs and outputs: `--bucket-name=my-custom-bucket`. If omitted, Google Cloud CLI uses or creates a default bucket named `PROJECT_ID-devicerun`.
- Because uploading large ZIP or IPA files can be time-consuming, you can directly reference your files using their Cloud Storage `gs://` paths to save upload time. Don't overwrite a shared `gs://` file right after submitting a session that references it, because Device Run stages `gs://` inputs asynchronously after submission.

> [!NOTE]
> **Note:** Test filtering is configured with `OnlyTestIdentifiers` and `SkipTestIdentifiers` in `.xctestrun` (or `--xctestrun-file`). Test discovery parses Mach-O symbols (`nm` / `swift-demangle`) rather than bytecode class hierarchies; so unlike Android JUnit4, it does not discover superclass-inherited test methods or parameterized annotation metadata (and only supports XCTest `test*` methods, not Swift Testing `@Test`).

### Synchronous and asynchronous runs

By default, the submit command is **blocking (synchronous)**. It will wait for
the test to complete and print a summary table of the results.

To submit the session and return immediately after uploading files to
Cloud Storage, add the `--async` flag:

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

This returns a session ID immediately. You can then wait for it using the
`sessions wait` command:

```bash
gcloud alpha device-run sessions wait SESSION_ID
```

The command will block until the job is completed.

### Multiple devices

To run the same test across multiple devices, provide the `--device` flag with
multiple device IDs separated by commas or with multiple `--device` flags, each
specifying a distinct device ID.

### Separated by commas

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

### Multiple flags

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

### Test timeout

Limit the execution duration of your test:

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

The valid range is `1m` to `1h` and defaults to `5m`.

### Software versions

Discover and use available software packages and versions of [Apple
Xcode](https://developer.apple.com/xcode/) and inspect their lifecycles,
supported versions, and metadata before configuring test runs.

First, list all available software packages and their versions:

```bash
gcloud alpha device-run software-versions list
```

Receiving output resembling:

    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

Then describe a specific package *using its exact resource ID* from the list:

```bash
gcloud alpha device-run software-versions describe xcode-16-4
```

Receiving output resembling:

    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'

Now that you know which software versions are available, you can use them in
your test runs with the `--xcode-version` flag, like so:

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

## Step 2. Choose device types

Using the `device-run` CLI, iOS tests can be executed across available physical
devices. To view the complete list of available devices visit either the
[interactive Device
Catalog](https://docs.cloud.google.com/developer-device-platform/device-catalog#available-devices) or run:

```bash
gcloud alpha device-run devices list
```

Example output:

    ID                MAKE   NAME           MODEL        HARDWARE_TYPE  OS_VERSION  CAPACITY  AVAILABILITY  PRODUCTS
    iphonese3-18-4    Apple  iPhone SE 3             iphonese3    PHYSICAL       18.4        NONE      NONE          Automation
    iphonese3-26-3    Apple  iPhone SE 3             iphonese3    PHYSICAL       26.3        NONE      NONE          Automation

See the Device Catalog to learn how to [filter this
list](https://docs.cloud.google.com/developer-device-platform/device-catalog#filter-devices) in the
Google Cloud CLI. To target a specific device for your test execution, use
its corresponding ID (ex. `iphonese3-18-4`) in the submit command.

## Step 3. Prepare and package your XCTest

Before running your tests, you must compile them, ensure they are signed, verify
the code signatures, and package the resulting artifacts into a ZIP file
containing your `.xctestrun` configuration and the application test bundles.

> [!TIP]
> **Tip:** **Migrating from Firebase Test Lab?** Device Run accepts the same test package as Firebase Test Lab: the `Debug-iphoneos/` directory and the `.xctestrun` file from `Build/Products`, as described in [Run an
> XCTest](https://docs.cloud.google.com/developer-device-platform/device-run/ios#run-test). If you already have a build and packaging pipeline for Firebase Test Lab, you can reuse it unchanged and pass the resulting `.zip` to `--test`. For the rest of the migration, see the iOS section of [Flag and
> command
> translation](https://docs.cloud.google.com/developer-device-platform/migrate/flag-command-translation#ios-flags).

### 1. Build your tests for physical iOS devices

Open your project in Xcode and build your app and test targets for testing:

1. Ensure that code signing is configured correctly in Xcode (specifying a valid provisioning profile and developer identity) for both the app target and the test runner target.
2. Build for testing using Xcode (*Product \> Build For \> Testing* ) or with
   `xcodebuild`.

   For a workspace:

   ```bash
   xcodebuild build-for-testing \
     -workspace YourProject.xcworkspace \
     -scheme YourScheme \
     -derivedDataPath ./build \
     -destination "generic/platform=iOS"
   ```

   For a standalone project:

   ```bash
   xcodebuild build-for-testing \
     -project YourProject.xcodeproj \
     -scheme YourScheme \
     -derivedDataPath ./build \
     -destination "generic/platform=iOS"
   ```

This generates the build products under: `./build/Build/Products`

> [!NOTE]
> **Note:** You don't need to register the Device Run device UDIDs in your Apple Developer account. Developer Device Platform re-signs your application and test bundles with a lab provisioning profile before installing them on the physical device, so a valid local development certificate and provisioning profile is all that is required to complete the build and packaging steps.

### 2. Verify build products and signatures

After building, verify that the application, runner, and `.xctestrun` manifest
were generated under `./build/Build/Products/`. If signed locally, you can
verify bundle signatures using `codesign`:

1. Verify the app bundle:

   ```bash
   codesign --verify --deep --verbose ./build/Build/Products/Debug-iphoneos/YourApp.app
   ```

   Expected output: `YourApp.app: valid on disk`
2. If running an XCUITest, also verify the test runner bundle:

   ```bash
   codesign --verify --deep --verbose ./build/Build/Products/Debug-iphoneos/YourApp-Runner.app
   ```

   Expected output: `YourApp-Runner.app: valid on disk`

> [!NOTE]
> **Note:** A separate `YourApp-Runner.app` bundle is produced only for UI test (XCUITest) targets. Application-hosted unit tests are embedded in the host app instead, and are covered by the verification of `YourApp.app`.

### 3. Create the ZIP archive

Compress both the `Debug-iphoneos` directory and the `.xctestrun` file located
in the build output products folder into a single ZIP archive.

> [!CAUTION]
> **Caution:** The Debug-iphoneos/ directory and the .xctestrun file must reside directly at the root of the ZIP archive. Do NOT zip the outer Products/ or build/ folder.

Run the following command from the build products directory:

```bash
cd ./build/Build/Products
zip -r MyTests.zip Debug-iphoneos *.xctestrun
```

This creates the packaged `MyTests.zip` that is ready to be used with the
`--test` flag.

## Step 4. Run your XCTest

Note these flags are required:

- *Device* : Specify a device using `--device`: `--device iphonese3-18-4`
- *Test* : Specify the test zip file using `--test`: `--test ./MyTests.zip`

To run your XCTest, use the `gcloud alpha device-run sessions submit xctest`
command. You must provide the test zip file (`MyTests.zip`) compiled in the
previous step.

To run a basic test session:

```bash
gcloud alpha device-run sessions submit xctest \
  --device iphonese3-18-4 \
  --test ./MyTests.zip
```

When execution begins, the CLI displays the created Session ID and the
Cloud Storage destination where results will be stored:

    Creating session [session-ee78b0ed] in location [global].
    Result files will be stored at [https://console.cloud.google.com/storage/browser/BUCKET_NAME/automation/sessions/session-ee78b0ed/].

    Waiting for session [session-ee78b0ed] to complete....

## Step 5. Use sharding

To include Developer Device Platform iOS XCTests in a Continuous Integration and
Continuous Delivery (CI/CD) workflow, consider sharding your tests. Test
sharding divides a suite of XCTests into subgroups (shards) that run separately
in isolation. Developer Device Platform automatically runs each shard in parallel
across multiple devices and completes the entire test suite in less time.

> [!CAUTION]
> **Caution:** Sharding tests can cost more. Although test sharding reduces total wall-clock completion time, installing and launching your app and test runner on each shard might increase total billed device time.

### Choose a sharding option

If your jobs have only a small number of test cases, or the total execution time
of all test cases is short, you don't need to use sharding. If you have a large
number of test cases, or the total execution time across all test cases is long,
consider using sharding.

Developer Device Platform supports both uniform and smart sharding in [`gcloud
alpha device-run sessions submit
xctest`](https://docs.cloud.google.com/sdk/gcloud/reference/alpha/device-run/sessions/submit/xctest). When
deciding how to shard your tests, consider the following options:

- If all test cases take a similar amount of time, use uniform sharding to
  divide test cases across a fixed number of shards.

- When the execution time varies significantly across test cases, use smart
  sharding. Developer Device Platform uses historical test execution timings to
  pack shards so that parallel shards finish around a target duration.

> [!IMPORTANT]
> **Important:** Because the number of shards is configurable, running many shards in parallel can be bottlenecked by device availability. Each entry in `--device` creates a separate job that runs N shards on that device model and OS pool. Keep the shard count per device model (and total `devices * shards`) within available lab capacity.

### Uniform sharding

To shard your XCTests with uniform sharding, include the
`--sharding-option=uniform` and `--uniform-sharding-count=` flags (valid range
is `1` to `20`) in the `gcloud alpha device-run sessions submit xctest` command:

```bash
gcloud alpha device-run sessions submit xctest \
    --test ./MyTests.zip \
    --device iphonese3-18-4,iphone16pro-18-3 \
    --sharding-option=uniform \
    --uniform-sharding-count=2
```

During execution, progress output displays `Job status: 2 running`.
Developer Device Platform creates one job for each specified device type, and
validates shared input artifacts once across identical jobs.

When the session completes, the final output lists each job and its shard
executions:

    Session [session-7b194c2e] finished with result [PASSED].
    JOB NAME  EXECUTION NAME  EXECUTION RESULT
    job-000   execution-000   PASSED
    job-000   execution-001   PASSED
    job-001   execution-000   PASSED
    job-001   execution-001   PASSED

### Smart sharding

To shard your XCTests with smart sharding, include the
`--sharding-option=smart`, `--smart-sharding-max-shard-count=`,
`--smart-sharding-target-duration=` (in minutes or `1h`), and
`--smart-sharding-record-name=` flags in the `gcloud alpha device-run sessions
submit xctest` command:

```bash
gcloud alpha device-run sessions submit xctest \
    --test ./MyTests.zip \
    --device iphonese3-18-4,iphonese3-26-3,iphone16pro-18-3 \
    --sharding-option=smart \
    --smart-sharding-max-shard-count=3 \
    --smart-sharding-target-duration=5m \
    --smart-sharding-record-name=xctest-suite
```

When the session completes, the final output lists each device job and the shard
executions allocated for that job:

    Session [session-3cd0564a] finished with result [PASSED].
    JOB NAME  EXECUTION NAME  EXECUTION RESULT
    job-000   execution-000   PASSED
    job-000   execution-001   PASSED
    job-001   execution-000   PASSED
    job-001   execution-001   PASSED
    job-002   execution-000   PASSED
    job-002   execution-001   PASSED

The following summary describes the smart sharding flags:

- `--smart-sharding-max-shard-count=SMART_SHARDING_MAX_SHARD_COUNT`:
  specify the maximum number of shards to create for smart sharding. If unset
  or set to `0`, system-defined maximum limits apply. The valid range is `0`
  to `20`.

- `--smart-sharding-target-duration=SMART_SHARDING_TARGET_DURATION`:
  specify the targeted execution time (for example, `2m`, `10m`, or `1h`) per
  shard for smart sharding. The valid range is `2m` to `1h`. Required when
  `--sharding-option=smart`.

-

    <code>--smart-sharding-record-name=<var>SMART_SHARDING_RECORD_NAME</var></code>:
    specify the name of the smart sharding record file, excluding the YAML file
    extension. Required when `--sharding-option=smart`. This YAML file is stored
    in the Cloud Storage bucket specified by `--bucket-name` (or
    <code><var>PROJECT_ID</var>-devicerun</code> by default, where
    <code><var>PROJECT_ID</var></code> is your Google Cloud project ID)
    under the `automation/smart-sharding/` directory. If the file doesn't exist,
    Developer Device Platform creates it automatically; otherwise,
    Developer Device Platform updates its contents when the session completes.

For more information about how the smart sharding algorithm calculates shard
assignments, the YAML timing record schema, and initial warm-up behavior, see
[Smart sharding in Developer Device Platform
Device Run](https://docs.cloud.google.com/developer-device-platform/learn/smart-sharding).

## Step 6. Explore and manage your test run

You can track active runs and manage historic sessions using these commands:

- *Describe session* : Query the live progress or final results of a session: `gcloud alpha device-run sessions describe SESSION_ID` Add `--full` to view complete details including device allocations, execution IDs, and Cloud Storage artifact paths.
- *List sessions* : View your recent runs: `gcloud alpha device-run sessions
  list --limit=10`
- *Cancel session* : Stop a running test session: `gcloud alpha device-run
  sessions cancel SESSION_ID`
- *Cancel operation* : Stop a pending or active operation: `gcloud alpha
  device-run operations cancel OPERATION_ID`

## What's next

Next up, [find and analyze logs](https://docs.cloud.google.com/developer-device-platform/device-run/find-logs.md.txt).