existential-birds/beagle / plugins/beagle-ios/skills/swift-testing-code-review
swift-testing-code-review
VerifiedReviews Swift Testing code for proper use of
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.3 KB |
| references/async-testing.md | markdown | 4.6 KB |
| references/expect-macro.md | markdown | 3.3 KB |
| references/organization.md | markdown | 4.2 KB |
| references/parameterized.md | markdown | 3.8 KB |
references/expect-macro.md
View on GitHub# #expect Macro
## Critical Anti-Patterns
### 1. Computing Booleans Outside #expect
```swift
// BAD - Loses expression capture and diagnostic context
let passed = user.age >= 18 && user.hasVerifiedEmail
#expect(passed)
// Failure shows: Expectation failed: passed
// GOOD - Full expression capture
#expect(user.age >= 18, "User must be adult")
#expect(user.hasVerifiedEmail, "Email verification required")
// Failure shows: (user.age → 16) >= 18
```
### 2. Overusing #require Instead of #expect
```swift
// BAD - Stops at first failure, hides other issues
@Test func testUserProfile() throws {
let user = try #require(fetchUser())
try #require(user.name == "Alice") // Stops here if fails
try #require(user.isActive) // Never checked
}
// GOOD - #require only for preconditions, #expect for assertions
@Test func testUserProfile() throws {
let user = try #require(fetchUser()) // Required to proceed
#expect(user.name == "Alice") // Soft check - continues
#expect(user.isActive) // Also checked
}
```
### 3. Mixing XCTest and Swift Testing
```swift
// BAD - Frameworks incompatible
@Test func testMixedFrameworks() {
XCTAssertEqual(value, expected) // WRONG
#expect(otherValue == expected)
}
// GOOD - Use one framework per test
@Test func testWithSwiftTesting() {
#expect(value == expected)
#expect(otherValue == expected)
}
```
### 4. Generic Error Testing
```swift
// BAD - Overly generic, masks specific failures
#expect(throws: (any Error).self) { try validate(input) }
// GOOD - Specific error case
#expect(throws: ValidationError.invalidFormat) { try validate(input) }
// GOOD - Custom validation for associated values
#expect(performing: { try validate(input) }, throws: { error in
guard let validationError = error as? ValidationError,
validationError.code == 400 else { return false }
return true
})
```
### 5. Force Unwrap After nil Check
```swift
// BAD - Assertion followed by force unwrap
#expect(optionalValue != nil)
let value = optionalValue!
// GOOD - Combine unwrap and assertion
let value = try #require(optionalValue)
```
## Best Practices
- **Embed expressions directly** in `#expect` for full diagnostic capture
- **Use `#expect`** for assertions (soft fail, continues), **`#require`** for preconditions (hard fail, stops)
- **Include descriptive messages** when failure reason isn't obvious
- **Test specific error cases** rather than generic `(any Error).self`
- **Implement `CustomTestStringConvertible`** for complex types to improve failure messages
## Migration from XCTest
| XCTest | Swift Testing |
|--------|---------------|
| `XCTAssertTrue(value)` | `#expect(value)` |
| `XCTAssertFalse(value)` | `#expect(!value)` |
| `XCTAssertNil(value)` | `#expect(value == nil)` |
| `XCTAssertNotNil(value)` | `#expect(value != nil)` |
| `XCTAssertEqual(a, b)` | `#expect(a == b)` |
| `XCTUnwrap(optional)` | `try #require(optional)` |
| `XCTFail(message)` | `Issue.record(message)` |
## Review Questions
1. Are expressions embedded directly in `#expect` for full capture?
2. Is `#require` used only for essential preconditions, not all assertions?
3. Are error tests checking specific error types, not generic `(any Error).self`?
4. Do complex types implement `CustomTestStringConvertible`?
5. Are assertion messages provided when failure reason isn't self-evident?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |