1.1 KiB
1.1 KiB
| paths | ||
|---|---|---|
|
Swift 測試 (Testing)
本檔案擴展了 common/testing.md,包含 Swift 特定內容。
框架 (Framework)
新測試請使用 Swift Testing (import Testing)。使用 @Test 與 #expect:
@Test("使用者建立時會驗證電子郵件")
func userCreationValidatesEmail() throws {
#expect(throws: ValidationError.invalidEmail) {
try User(email: "not-an-email")
}
}
測試隔離 (Test Isolation)
每個測試都會獲得一個新的實例 — 在 init 中設定,在 deinit 中清理。測試之間不存在共享的可變狀態。
參數化測試 (Parameterized Tests)
@Test("驗證格式", arguments: ["json", "xml", "csv"])
func validatesFormat(format: String) throws {
let parser = try Parser(format: format)
#expect(parser.isValid)
}
覆蓋率 (Coverage)
swift test --enable-code-coverage
參考資源
參見技能 (Skill):swift-protocol-di-testing,獲取基於協定的依賴注入以及搭配 Swift Testing 的 Mock 模式。