qa-testing-ios
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseQA Testing (iOS)
iOS QA测试
Use + Xcode Simulator () to build, run, and stabilize iOS tests.
xcodebuildsimctl使用 + Xcode Simulator () 来构建、运行并稳定iOS测试。
xcodebuildsimctlInputs to Confirm
需要确认的输入项
- Xcode entrypoint: or
-workspace-project - (and optional
-scheme)-testPlan - Destination(s): simulator name + iOS runtime (or ), and whether real devices are required
OS=latest - UI-test hooks: launch arguments/env toggles (stubs, demo data, auth bypass, disable animations)
- Artifact needs: , coverage, screenshots/video, logs
xcresult
- Xcode入口:或
-workspace-project - (可选
-scheme)-testPlan - 测试目标:模拟器名称 + iOS运行时(或),以及是否需要真机
OS=latest - UI测试钩子:启动参数/环境变量开关(桩数据、演示数据、跳过认证、禁用动画)
- 产物需求:、测试覆盖率、截图/录屏、日志
xcresult
Quick Commands
快速命令
| Task | Command |
|---|---|
| List schemes | |
| List simulators | |
| List devices (USB) | |
| Boot simulator | |
| Wait for boot | |
| Build app | |
| Install app | |
| Run tests | |
| Run tests (device) | |
| Reset simulators | |
| Take screenshot | |
| Record video | |
| 任务 | 命令 |
|---|---|
| 列出Scheme | |
| 列出模拟器 | |
| 列出USB连接设备 | |
| 启动模拟器 | |
| 等待模拟器启动完成 | |
| 构建应用 | |
| 安装应用 | |
| 运行测试 | |
| 在真机上运行测试 | |
| 重置模拟器 | |
| 截图 | |
| 录屏 | |
Workflow
工作流程
- Resolve build inputs (,
workspace/project,scheme, destinations).testPlan - Make simulator state repeatable: shutdown/erase as needed, boot, and wait for boot.
- Run tests with artifacts enabled (); parallelize and retry only when appropriate.
-resultBundlePath - Triage failures from the bundle; confirm flakes with repetition; quarantine with an owner and reproduction steps.
xcresult
- 确定构建输入项(、
workspace/project、scheme、测试目标)。testPlan - 确保模拟器状态可重复:按需关闭/重置模拟器,启动并等待启动完成。
- 启用产物输出()运行测试;仅在合适时启用并行测试和重试。
-resultBundlePath - 从包中排查失败用例;通过重复执行确认是否为不稳定测试;标记问题并分配责任人,同时附上复现步骤。
xcresult
xcodebuild Patterns
Xcodebuild使用模式
- Select tests to reproduce: and
-only-testing:TargetTests/ClassName/testMethod.-skip-testing:TargetTests/FlakyClass - Prefer test plans for large suites: (keeps device/config/runs consistent).
-testPlan <plan> - Enable parallel testing when suites are isolation-safe: (+
-parallel-testing-enabled YES).-maximum-parallel-testing-workers N - Always write a result bundle in automation: .
-resultBundlePath TestResults.xcresult - For reruns, split build and test: then
xcodebuild build-for-testing ....xcodebuild test-without-building ... - Inspect results locally: or
open TestResults.xcresult.xcrun xcresulttool get --path TestResults.xcresult --format json
- 选择特定测试进行复现:和
-only-testing:TargetTests/ClassName/testMethod。-skip-testing:TargetTests/FlakyClass - 大型测试套件优先使用测试计划:(确保设备/配置/执行保持一致)。
-testPlan <plan> - 当测试套件支持隔离时,启用并行测试:(+
-parallel-testing-enabled YES)。-maximum-parallel-testing-workers N - 自动化测试中始终生成结果包:。
-resultBundlePath TestResults.xcresult - 重跑测试时,拆分构建和测试步骤:然后
xcodebuild build-for-testing ...。xcodebuild test-without-building ... - 本地查看测试结果:或
open TestResults.xcresult。xcrun xcresulttool get --path TestResults.xcresult --format json
Flake Triage (Repetition and Retry)
不稳定测试排查(重复执行与重试)
- Prefer repetition to prove flake rate before adding retries.
- Use targeted reruns before suite-wide retries.
Common patterns (flags vary by Xcode version):
- Retry failing tests once in CI:
-retry-tests-on-failure -test-iterations 2 - Measure flakiness until first failure:
-test-iterations 50 -test-repetition-mode until-failure - Run a single test repeatedly:
-only-testing:TargetTests/ClassName/testMethod -test-iterations 20
- 在添加重试逻辑前,优先通过重复执行确认不稳定率。
- 优先针对特定测试重跑,而非全套件重试。
常见配置(参数随Xcode版本变化):
- CI中失败测试重试一次:
-retry-tests-on-failure -test-iterations 2 - 重复执行直到首次失败以测量不稳定率:
-test-iterations 50 -test-repetition-mode until-failure - 重复执行单个测试:
-only-testing:TargetTests/ClassName/testMethod -test-iterations 20
Testing Layers
测试层级
| Layer | Framework | Scope |
|---|---|---|
| Unit | XCTest / Swift Testing | Business logic (fast) |
| Snapshot | XCTest + snapshot libs | View rendering |
| Integration | XCTest | Persistence, networking |
| UI | XCUITest | Critical user journeys |
| 测试层级 | 框架 | 测试范围 |
|---|---|---|
| 单元测试 | XCTest / Swift Testing | 业务逻辑(执行快速) |
| 快照测试 | XCTest + 快照库 | 视图渲染效果 |
| 集成测试 | XCTest | 持久化、网络交互 |
| UI测试 | XCUITest | 核心用户流程 |
Device Matrix
设备矩阵策略
- Default: simulators for PR gates; real devices for release
- Cover: one small phone, one large phone, iPad if supported
- Add OS versions only for multiple major release support
- 默认:PR校验使用模拟器;发布阶段使用真机
- 覆盖范围:一台小屏手机、一台大屏手机,若支持则包含iPad
- 仅在需要支持多个主要版本时,添加对应OS版本测试
Flake Control
不稳定测试控制
Use these defaults unless the project requires otherwise:
- Disable or reduce animations in UI-test builds.
- Fix locale/timezone (via launch arguments or app-level configuration).
- Stub network at the boundary (avoid real third-party calls in UI tests).
- Reset app state between tests (fresh install, deep-link reset, or explicit teardown).
- Prefer state-based waits (, expectations) over sleeps.
waitForExistence - Pre-grant/reset permissions where possible (simulators): .
xcrun simctl privacy booted grant ...
除非项目有特殊需求,否则默认遵循以下规则:
- 在UI测试构建中禁用或减少动画。
- 固定区域设置/时区(通过启动参数或应用级配置)。
- 在边界层Stub网络请求(UI测试中避免真实第三方调用)。
- 测试间重置应用状态(重新安装、通过Deep Link重置,或显式清理)。
- 优先使用基于状态的等待(、预期断言)而非休眠。
waitForExistence - 尽可能提前授予/重置权限(模拟器):。
xcrun simctl privacy booted grant ...
CI Integration (GitHub Actions)
CI集成(GitHub Actions)
yaml
name: iOS CI
on: [push, pull_request]
jobs:
test:
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "16.0"
- run: |
set -euo pipefail
xcodebuild test \
-scheme MyApp \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 15 Pro,OS=latest' \
-resultBundlePath TestResults.xcresult
- uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: TestResults.xcresultyaml
name: iOS CI
on: [push, pull_request]
jobs:
test:
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "16.0"
- run: |
set -euo pipefail
xcodebuild test \
-scheme MyApp \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 15 Pro,OS=latest' \
-resultBundlePath TestResults.xcresult
- uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: TestResults.xcresultDo / Avoid
建议/避免事项
Do
建议
- Make UI tests independent and idempotent
- Use test data builders and dedicated test accounts
- Collect bundles on failure
xcresult - Use accessibilityIdentifier, not labels
- 确保UI测试独立且幂等
- 使用测试数据构建器和专用测试账号
- 测试失败时收集包
xcresult - 使用accessibilityIdentifier而非文本标签定位元素
Avoid
避免
- Relying on test ordering or global state
- UI tests requiring real network
- Thread.sleep() for synchronization
- Accepting AI-proposed selectors without validation
- 依赖测试执行顺序或全局状态
- UI测试依赖真实网络请求
- 使用Thread.sleep()进行同步
- 直接使用AI生成的元素选择器而不验证
Resources
资源
| Resource | Purpose |
|---|---|
| references/swift-testing.md | Swift Testing framework |
| references/simulator-commands.md | Complete simctl reference |
| references/xctest-patterns.md | XCTest/XCUITest patterns |
| 资源 | 用途 |
|---|---|
| references/swift-testing.md | Swift Testing框架相关文档 |
| references/simulator-commands.md | 完整simctl命令参考 |
| references/xctest-patterns.md | XCTest/XCUITest使用模式 |
Templates
模板
| Template | Purpose |
|---|---|
| assets/template-ios-ui-test-stability-checklist.md | Stability checklist |
| 模板 | 用途 |
|---|---|
| assets/template-ios-ui-test-stability-checklist.md | UI测试稳定性检查清单 |
Related Skills
相关技能
| Skill | Purpose |
|---|---|
| software-mobile | iOS development |
| qa-testing-strategy | Test strategy |
| qa-testing-mobile | Cross-platform mobile |
| 技能 | 用途 |
|---|---|
| software-mobile | iOS开发 |
| qa-testing-strategy | 测试策略制定 |
| qa-testing-mobile | 跨平台移动测试 |