闽公网安备 35020302035485号
4.控制执行顺序
@Test(.disabled("这测试太不稳定"))
func someFlakyTest() {
// 测试代码
}
比注释掉代码优雅多了。struct TestHelper {
staticvar isSimulator: Bool {
#if targetEnvironment(simulator)
returntrue
#else
returnfalse
#endif
}
}
@Test(.disabled(if: TestHelper.isSimulator,
"推送测试只能真机"))
func testPushNotification() {
// 推送相关测试
}
模拟器自动跳过,不报错不浪费时间。@Test(.enabled(if: ProcessInfo.processInfo.environment["CI"] != nil,
"CI 环境才跑"))
func testCIFeature() {
// 堆代码 duidaima.com
// CI 专属测试
}
关联 Bug@Test(
.disabled("等后端修复接口"),
.bug("https://github.com/YourProject/issues/42")
)
func testLoginAPI() {
// 登录接口测试
}
还能加 bug ID 和描述:@Test(.bug("https://github.com/Project/issues/517",
id: 517,
"头像上传失败"))
func testAvatarUpload() {
let result = uploadAvatar(image: testImage)
#expect(result.isSuccess) // 先写测试重现 bug
}
支持各种 bug 系统,包括 Apple 的:@Test(.bug(id: "FB12345")) // Feedback Assistant
func testSystemBug() {
// 系统级 bug
}
测试报告里这些链接都能点。@Test(.timeLimit(.minutes(10)))
func testFileUpload() {
let file = createLargeFile(sizeInMB: 100)
let result = uploadFile(file)
#expect(result.isSuccess)
}
注意两点:@Test(.timeLimit(.seconds(5)),
arguments: [1, 10, 100, 1000])
func testProcessing(itemCount: Int) {
// 每个参数都有 5 秒
processItems(count: itemCount)
}
串行执行@Suite(.serialized)
struct DatabaseTests {
@Test func createUser() { }
@Test func deleteUser() { }
@Test func updateUser() { }
}
加 .serialized 顺序执行,避免冲突。.Mock 真实数据库
@Test(
.disabled(if: isProduction, "生产环境不跑"),
.bug("JIRA-1234", "已知问题"),
.timeLimit(.seconds(30))
)
func testDangerousOperation() {
// 危险操作
}
2. 测试分组// 慢速套件
@Suite(.serialized)
struct SlowTests {
@Test(.timeLimit(.minutes(5)))
func testHeavyComputation() { }
}
// 快速套件
struct FastTests {
@Test(.timeLimit(.seconds(1)))
func testQuickValidation() { }
}
3. CI/CD 集成let isCI = ProcessInfo.processInfo.environment["CI"] != nil
let isPR = ProcessInfo.processInfo.environment["GITHUB_EVENT_NAME"] == "pull_request"
@Test(.disabled(if: !isCI, "只在 CI 跑"))
func testCIOnlyFeature() { }
@Test(.disabled(if: isPR, "PR 不跑集成测试"))
func testIntegration() { }
条件判断要准:错了重要测试可能被跳过