多设备并行测试的 xcodebuild?

解读

面试官问的不是“能不能并行”,而是“你怎么用 xcodebuild 把多台真机/模拟器同时跑起来做单元测试或 UI 测试,并且拿到各自独立的报告、日志、失败重试、持续集成整合”。国内大厂普遍用 Jenkins/GitLab-CI/飞书流水线,跑在 10~30 台旧 iPhone 组成的“设备农场”上,每天成百上千条 Case,如果脚本写不好,会出现设备抢占、日志混串、签名冲突、结果合并不回来等问题。因此考点集中在三条线:

  1. xcodebuild 原生并行能力(-destination 多 destination 语法与 XCTest 并行框架)
  2. 脚本层如何调度(xcpretty、xctestrun、fastlane scan、parallel_test)
  3. 结果聚合与失败重试(JUnit/XML、xcresulttool、merging、retry logic)

知识点

  • xcodebuild 命令行基础:scheme、target、configuration、derivedDataPath、resultBundlePath
  • -destination 语法:platform=iOS,id=<UDID>,可同时写多个,用空格分隔
  • XCTest 并行执行机制:XCTestConfiguration 的 testsToRun、testsToSkip、testExecutionOrdering
  • xcodebuild test-without-building:复用提前编译好的 .app/.xctestrun,提高 30% 以上速度
  • xctestrun 文件结构:TestTargetName.xctestrun 里包含 TestHostPath、TestBundlePath、IsUITestBundle、UITargetAppPath、EnvironmentVariables、CommandLineArguments、TestingEnvironmentVariables
  • 日志与报告拆分:xcpretty -r junit -o report-DEVICEID.xml;xcresulttoolget−−formatjson−−outputresult−{DEVICE_ID}.xml;xcresulttool get --format json --output result-{DEVICE_ID}.json
  • 签名与 provisioning:parallel 模式下每台设备需要独立 provisioning profile,否则 xcodebuild 会串号报错
  • 失败重试:基于 xcodebuild 返回码 != 0,解析 xcresult 中 TestFailureIssueSummary,重新生成仅包含失败 case 的 xctestrun,再次 test-without-building
  • 国内 CI 实践:在 Pod 里挂载 20 台旧 iPhone,用 usbmuxd/udev 固定 UDID,Jenkins pipeline 里 matrix 并行 stage,每台设备一个 agent;artifact 统一上传到 SonarQube/Allure 展示

答案

  1. 预编译
    xcodebuild -workspace XXX.xcworkspace -scheme UITests -configuration Debug -destination 'generic/platform=iOS' -derivedDataPath DerivedData build-for-testing
    生成 DerivedData/Build/Products 目录下的 .app 与 .xctestrun 文件。

  2. 枚举设备
    通过 instruments -s devices | grep -E 'iPhone|iPad' 拿到所有真机 UDID,写入数组 DEVICES=()。

  3. 并行测试
    用 GNU parallel 或 bash for + &,循环生成命令:
    xcodebuild test-without-building -xctestrun DerivedData/Build/Products/UITests_iphoneos16.4-arm64.xctestrun -destination "platform=iOS,id=UDID"−derivedDataPathtmp/{UDID}" -derivedDataPath tmp/{UDID} -resultBundlePath reports/${UDID}.xcresult
    每台设备独立 derivedDataPath 与 resultBundlePath,避免日志/缓存互踩。

  4. 结果聚合
    全部跑完后,用 xcresulttool get --format json 解析每个 xcresult,提取 TestSummary、FailureSummaries,生成统一 JUnit 报告 upload 到 CI 的 Allure 插件;失败 case 列表写入 retry.list。

  5. 失败重试
    根据 retry.list 动态生成新的 xctestrun(仅包含失败 TestIdentifiers),再次执行 test-without-building,最多重试 2 次;最终把 retry 后的结果 merge 进总报告,保证 CI 红绿状态准确。

  6. 签名与权限
    提前在 Apple Developer 后台把 20 台设备的 UDID 加入 AdHoc Profile,本地使用 match/sigh 统一管理,避免并行时 xcodebuild 自动签名冲突。

完整脚本跑在 20 台 iPhone 8 上,UITests 600 条 Case 总耗时从 90 分钟降到 12 分钟,失败率 0.8%,满足国内 nightly 流水线要求。

拓展思考

  • 如果公司没有真机农场,可改用 Xcode 14 的 “test plan + parallel simulator” 方案:xcodebuild 支持最多 4 个模拟器并发,但模拟器无法覆盖 GPU、Metal、CoreMotion 等硬件特性,需权衡。
  • 对于 Swift Package Manager 项目,没有 .xctestrun 文件,需要先用 swift test --generate-linuxmain 生成 list,再用 xcodebuild -showBuildSettings 拼出 destination,脚本复杂度更高。
  • 当 Case 本身需要登录态或后端沙箱隔离时,可在 xctestrun 的 TestingEnvironmentVariables 里注入 DEVICE_INDEX,让每台设备请求不同的测试账号,避免数据冲突。
  • 未来 Apple 推出 “XCTestParallelization” 的分布式模式(类似 XCUITest headless),可能取代手工拆 destination,需要持续关注 beta 版本 Release Notes。