.agents/skills/swift-testing-expert/references/expectations.md
Use this file when writing assertions, migrating from XCTAssert*, testing thrown errors, or documenting known failures.
#expect as the default#expect for most assertions.==, >, .contains, .isEmpty, etc.).import Testing
@Test func pricingRules() {
let subtotal = 25
let discount = 5
let total = subtotal - discount
#expect(total == 20)
#expect(total > 0)
#expect([10, 20, 30].contains(total))
}
#require for prerequisitestry #require(...) when later assertions depend on this condition.#require as "guard + fail test early".import Testing
@Test func parsedURLHasHTTPS() throws {
let value = "https://www.avanderlee.com"
let url = try #require(URL(string: value), "URL should parse")
#expect(url.scheme == "https")
}
do/catch unless custom branching is truly needed.import Testing
enum BrewError: Error, Equatable {
case missingBeans
}
func brew(_ hasBeans: Bool) throws -> String {
guard hasBeans else { throw BrewError.missingBeans }
return "coffee"
}
@Test func expectedThrows() {
#expect(throws: BrewError.self) {
try brew(false)
}
}
@Test func expectedNoThrow() {
#expect(throws: Never.self) {
try brew(true)
}
}
withKnownIssue for temporary expected failures you still want to compile/run.withKnownIssue over blanket disabling when you need ongoing visibility.import Testing
@Test func checkoutFlow() {
#expect(true) // still validated
withKnownIssue("Checkout backend intermittently returns 503", isIntermittent: true) {
Issue.record("Known upstream issue")
}
#expect(2 + 2 == 4) // rest of test still executes
}
CustomTestStringConvertible for concise test output.CustomStringConvertible separate from test-specific descriptions when needed.import Testing
struct Receipt: CustomTestStringConvertible {
let id: UUID
let total: Decimal
var testDescription: String {
"Receipt(total: \(total))"
}
}
// XCTAssertEqual(total, 20)
#expect(total == 20)
// try XCTUnwrap(user)
let user = try #require(user)
// XCTFail("Unreachable")
Issue.record("Unreachable")
#require when later checks depend on a value.withKnownIssue scopes narrow.