SuperAPI AI 工具集

axiom-testing-async

测试与质量

查看项目

更新于 10-16 00:23

技能介绍

用途:为使用 Swift Testing 框架测试异步/await 代码提供现代模式,覆盖回调、MainActor 隔离代码与 XCTest 迁移场景。不适用于纯 XCTest 项目、UI 自动化测试(XCUITest)和带指标的性能测试。

主要能力与写法:

  • 简单异步函数用 @Test 配合 async throws 与 #expect 断言。
  • 无 async 重载的旧 API 用 withCheckedThrowingContinuation 包装回调。
  • 回调验证用 await confirmation { confirm in ... },期望多次可传 expectedCount,验证从不触发可用 expectedCount: 0。
  • MainActor 隔离代码显式标注 @Test @MainActor。
  • 超时用 @Test(.timeLimit(.seconds(5)))。
  • 抛错用 await #expect(throws:)。
  • 可选值解包用 try #require。
  • 参数化异步测试用 @Test(arguments:),参数自动并行执行。

并行与共享状态:Swift Testing 默认并行执行(XCTest 默认串行)。共享可变状态会产生数据竞争,应让每个测试使用新实例;必须顺序执行时用 @Suite(..., .serialized)。

迁移对照:XCTestExpectation 对应 confirmation;wait(for:timeout:) 对应 await confirmation;XCTAssertEqual 对应 #expect;async setUp 对应 Suite 的 init。

常见错误:用 Task.sleep 代替 confirmation 导致不稳定;UI 测试漏标 @MainActor;回调测试缺少 confirmation 导致测试提前结束;未处理并行执行造成测试互相干扰。

限制:资料未提供安装命令、价格或平台要求,相关细节暂未提供。