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

iOS中基于XCUITest实现分布式并行测试的方案咨询

分布式XCUITest并行测试解决方案

针对你需要4台设备分配6个测试类、每个类仅执行一次的需求,以下是几个可行的落地方案:

方案一:基于xcodebuild的Shell脚本分发

这是最直接可控的方案,通过给每个设备单独指定要运行的测试类,并行启动测试进程。

步骤1:定义设备与测试类映射

先收集每台iPhone的UDID,然后绑定对应的测试类:

  • iPhone1(UDID:xxx1):Class1、Class5
  • iPhone2(UDID:xxx2):Class2、Class6
  • iPhone3(UDID:xxx3):Class3
  • iPhone4(UDID:xxx4):Class4

步骤2:编写并行执行脚本

创建run_distributed_tests.sh脚本,后台并行启动每个设备的测试命令:

#!/bin/bash

# 设备UDID -> 测试类列表的映射(替换为你的实际值)
declare -A device_test_map=(
  ["xxx1"]="MyAppUITests/Class1 MyAppUITests/Class5"
  ["xxx2"]="MyAppUITests/Class2 MyAppUITests/Class6"
  ["xxx3"]="MyAppUITests/Class3"
  ["xxx4"]="MyAppUITests/Class4"
)

# 遍历设备,启动测试进程
for udid in "${!device_test_map[@]}"; do
  test_classes=${device_test_map[$udid]}
  only_testing_args=""
  
  # 拼接-only-testing参数
  for class in $test_classes; do
    only_testing_args="$only_testing_args -only-testing:$class"
  done

  # 后台执行测试,日志单独保存
  xcodebuild test-without-building \
    -scheme MyApp \
    -destination "id=$udid" \
    $only_testing_args > "test_log_$udid.log" 2>&1 &
done

# 等待所有测试进程完成
wait

echo "所有设备测试执行完毕"

执行脚本

给脚本添加执行权限后运行:

chmod +x run_distributed_tests.sh
./run_distributed_tests.sh

方案二:基于Fastlane的并行lane配置

利用Fastlane的parallel功能,为每个设备单独定义测试lane,并行执行。

Fastfile配置示例

default_platform(:ios)

platform :ios do
  # 对应iPhone1的测试lane
  lane :test_iphone1 do
    scan(
      scheme: "MyApp",
      destination: "id=xxx1",
      only_testing: ["MyAppUITests/Class1", "MyAppUITests/Class5"],
      output_directory: "./test_results/iphone1"
    )
  end

  # 对应iPhone2的测试lane
  lane :test_iphone2 do
    scan(
      scheme: "MyApp",
      destination: "id=xxx2",
      only_testing: ["MyAppUITests/Class2", "MyAppUITests/Class6"],
      output_directory: "./test_results/iphone2"
    )
  end

  # 对应iPhone3的测试lane
  lane :test_iphone3 do
    scan(
      scheme: "MyApp",
      destination: "id=xxx3",
      only_testing: ["MyAppUITests/Class3"],
      output_directory: "./test_results/iphone3"
    )
  end

  # 对应iPhone4的测试lane
  lane :test_iphone4 do
    scan(
      scheme: "MyApp",
      destination: "id=xxx4",
      only_testing: ["MyAppUITests/Class4"],
      output_directory: "./test_results/iphone4"
    )
  end

  # 并行执行所有设备的测试
  lane :run_distributed_tests do
    parallel(
      test_iphone1: {},
      test_iphone2: {},
      test_iphone3: {},
      test_iphone4: {}
    )
  end
end

启动测试

执行以下命令即可并行启动所有设备的测试:

fastlane run_distributed_tests

方案三:自定义Xcode Test Plan分组

通过Test Plan的分组和配置,给每个设备分配专属测试类:

  1. 打开Xcode Test Plan,创建4个测试分组(比如Group1~Group4),分别放入对应的测试类;
  2. 在Test Plan的「Configurations」选项卡,为每个设备创建独立配置,每个配置仅启用对应的分组;
  3. 运行Test Plan时,选择所有设备对应的配置,确保每个设备仅执行自身分组内的测试类。

关键注意事项

  • 测试类路径格式必须正确:测试Target名称/测试类名称,比如MyAppUITests/Class1;
  • 使用test-without-building可跳过重复构建,提升执行效率(需确保已提前构建测试目标);
  • 每个设备的测试日志单独保存,方便后续排查问题。

内容的提问来源于stack exchange,提问作者Prashant Kumar Manjhi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 21:13:14