Agent skills for iOS, iPadOS, Swift, SwiftUI, and modern Apple framework development.
71
89%
Does it follow best practices?
Impact
—
No eval scenarios have been run
Advisory
Suggest reviewing before use
import Testing
@Test("User can update their display name")
func updateDisplayName() {
var user = User(name: "Alice")
user.name = "Bob"
#expect(user.name == "Bob")
}
@Test(.tags(.validation, .email))
func validatesEmailFormat() { /* ... */ }#expect(result == 42)
#expect(name.isEmpty == false)
#expect(items.count > 0, "Items should not be empty")
// Error type checking
#expect(throws: ValidationError.self) {
try validate(email: "not-an-email")
}
// Specific error matching
#expect {
try validate(email: "")
} throws: { error in
guard let err = error as? ValidationError else { return false }
return err == .empty
}
// #require unwraps or fails the test
let user = try #require(await fetchUser(id: 1))
let first = try #require(items.first)Rule: Use #require when subsequent assertions depend on the value. Use #expect for independent checks.
@Suite("User Authentication")
struct AuthTests {
let service: AuthService
let mockRepo: MockUserRepository
// init() replaces setUp() -- runs before each test
init() {
mockRepo = MockUserRepository()
service = AuthService(repository: mockRepo)
}
@Test func loginSucceeds() async throws {
let user = try await service.login(email: "test@test.com", password: "pass")
#expect(user.email == "test@test.com")
}
@Test func loginFailsWithBadPassword() async {
#expect(throws: AuthError.invalidCredentials) {
try await service.login(email: "test@test.com", password: "wrong")
}
}
}Suites can nest for logical grouping:
@Suite("Payments")
struct PaymentTests {
@Suite("Subscriptions")
struct SubscriptionTests {
@Test func renewsAutomatically() { /* ... */ }
}
@Suite("One-Time")
struct OneTimeTests {
@Test func chargesCorrectAmount() { /* ... */ }
}
}@Test("Email validation", arguments: [
("user@example.com", true),
("user@", false),
("@example.com", false),
("", false),
])
func validateEmail(email: String, isValid: Bool) {
#expect(EmailValidator.isValid(email) == isValid)
}
// From CaseIterable
@Test(arguments: Currency.allCases)
func currencyHasSymbol(currency: Currency) {
#expect(currency.symbol.isEmpty == false)
}
// Two collections: cartesian product
@Test(arguments: [1, 2, 3], ["a", "b"])
func combinations(number: Int, letter: String) {
#expect(number > 0)
}
// Use zip for 1:1 pairing
@Test(arguments: zip(["USD", "EUR"], ["$", "€"]))
func currencySymbols(code: String, symbol: String) {
#expect(Currency(code: code).symbol == symbol)
}Each argument combination runs as an independent test case reported separately.
Swift Testing uses Swift Concurrency and runs tests in parallel by default. Treat every test as isolated work unless you explicitly serialize a scope.
@Suite(.serialized, .tags(.database))
struct DatabaseTests {
@Test func insertsRecord() async throws { /* ... */ }
@Test func removesRecord() async throws { /* ... */ }
}Use .serialized when tests must not overlap because they touch shared external state like a keychain, database, singleton service, or filesystem location. It does not make unrelated tests outside the serialized scope run one-at-a-time.
Important implications:
// Basic confirmation -- event must fire exactly once
await confirmation("Received notification") { confirm in
let observer = NotificationCenter.default.addObserver(
forName: .userLoggedIn, object: nil, queue: .main
) { _ in confirm() }
await authService.login()
NotificationCenter.default.removeObserver(observer)
}
// Expected count -- event must fire exactly N times
await confirmation("Received 3 items", expectedCount: 3) { confirm in
processor.onItem = { _ in confirm() }
await processor.process(items)
}// Known failing test -- does not count as failure
withKnownIssue("Propane tank is empty") {
#expect(truck.grill.isHeating)
}
// Intermittent / flaky
withKnownIssue(isIntermittent: true) {
#expect(service.isReachable)
}
// Conditional
withKnownIssue {
#expect(foodTruck.grill.isHeating)
} when: {
!hasPropane
}
// Match specific issues only
try withKnownIssue {
let level = try #require(foodTruck.batteryLevel)
#expect(level >= 0.8)
} matching: { issue in
guard case .expectationFailed(let expectation) = issue.kind else { return false }
return expectation.isRequired
}If no known issues are recorded, Swift Testing records a distinct issue notifying you the problem may be resolved.
Tags must be declared as static members in an extension on Tag:
extension Tag {
@Tag static var critical: Self
@Tag static var slow: Self
@Tag static var networking: Self
@Tag static var validation: Self
}
@Test(.tags(.critical, .networking))
func apiCallReturnsData() async throws { /* ... */ }Filter tests by tag in Xcode test plans or CLI (tag-based filtering syntax varies by toolchain — verify for your Swift version).
TestScoping consolidates per-test setup/teardown into reusable fixtures when attached through a custom trait:
struct DatabaseScope: TestTrait, SuiteTrait, TestScoping {
func provideScope(
for test: Test,
testCase: Test.Case?,
performing body: @Sendable () async throws -> Void
) async throws {
let db = try await TestDatabase.create()
defer { Task { try? await db.destroy() } }
try await body()
}
}
extension Trait where Self == DatabaseScope {
static var databaseScope: Self { .init() }
}
@Test(.databaseScope, .tags(.database))
func insertsRecord() async throws {
// Test runs inside DatabaseScope.provideScope
}Define testable boundaries with protocols:
protocol UserRepository: Sendable {
func fetch(id: String) async throws -> User
func save(_ user: User) async throws
}
struct MockUserRepository: UserRepository {
var users: [String: User] = [:]
var fetchError: Error?
var savedUsers: [User] = []
func fetch(id: String) async throws -> User {
if let error = fetchError { throw error }
guard let user = users[id] else { throw NotFoundError() }
return user
}
mutating func save(_ user: User) async throws {
savedUsers.append(user)
users[user.id] = user
}
}Pattern: Mocks conform to protocols, never subclass concrete types. Store call counts and arguments for verification.
Inject dependencies through initializers for testability:
@Observable
class ProfileViewModel {
var user: User?
var error: Error?
private let repository: UserRepository
init(repository: UserRepository) {
self.repository = repository
}
func load() async {
do {
user = try await repository.fetch(id: "current")
} catch {
self.error = error
}
}
}
// Test with mock
@Test @MainActor func viewModelLoadsUser() async {
let mock = MockUserRepository(users: ["current": .preview])
let vm = ProfileViewModel(repository: mock)
await vm.load()
#expect(vm.user?.name == "Alice")
}
@Test @MainActor func viewModelHandlesError() async {
var mock = MockUserRepository()
mock.fetchError = URLError(.notConnectedToInternet)
let vm = ProfileViewModel(repository: mock)
await vm.load()
#expect(vm.user == nil)
#expect(vm.error != nil)
}@Test @MainActor func viewModelUpdatesOnMainActor() async {
let vm = ProfileViewModel(repository: MockUserRepository())
await vm.load()
#expect(vm.user != nil)
}
// Clock injection for time-dependent logic
@Test func debounceUsesCorrectDelay() async throws {
let clock = TestClock()
let debouncer = Debouncer(delay: .seconds(1), clock: clock)
debouncer.submit { /* action */ }
await clock.advance(by: .milliseconds(500))
#expect(!debouncer.hasExecuted)
await clock.advance(by: .milliseconds(500))
#expect(debouncer.hasExecuted)
}
// Error path testing
@Test func fetchThrowsOnNetworkError() async {
var mock = MockUserRepository()
mock.fetchError = URLError(.notConnectedToInternet)
#expect(throws: URLError.self) {
try await mock.fetch(id: "1")
}
}Swift Testing does not support UI testing. Use XCTest with XCUITest for all UI tests.
class LoginUITests: XCTestCase {
let app = XCUIApplication()
override func setUpWithError() throws {
continueAfterFailure = false
app.launch()
}
func testLoginFlow() throws {
let loginPage = LoginPage(app: app)
let homePage = loginPage.login(email: "test@test.com", password: "password")
XCTAssertTrue(homePage.welcomeLabel.exists)
}
}Encapsulate UI element queries in page objects for reusable, readable UI tests:
struct LoginPage {
let app: XCUIApplication
var emailField: XCUIElement { app.textFields["Email"] }
var passwordField: XCUIElement { app.secureTextFields["Password"] }
var signInButton: XCUIElement { app.buttons["Sign In"] }
@discardableResult
func login(email: String, password: String) -> HomePage {
emailField.tap(); emailField.typeText(email)
passwordField.tap(); passwordField.typeText(password)
signInButton.tap()
return HomePage(app: app)
}
}
struct HomePage {
let app: XCUIApplication
var welcomeLabel: XCUIElement { app.staticTexts["Welcome"] }
}class FeedPerformanceTests: XCTestCase {
func testFeedParsingPerformance() throws {
let data = try loadFixture("large-feed.json")
let metrics: [XCTMetric] = [XCTClockMetric(), XCTMemoryMetric()]
measure(metrics: metrics) {
_ = try? FeedParser.parse(data)
}
}
}Performance tests require XCTest — not available in Swift Testing.
Add Point-Free's SnapshotTesting package to the test target via Swift Package Manager, then use it for visual regression. Requires XCTest:
import SnapshotTesting
import XCTest
class ProfileViewSnapshotTests: XCTestCase {
func testProfileView() {
let view = ProfileView(user: .preview)
assertSnapshot(of: view, as: .image(layout: .device(config: .iPhone13)))
// Dark mode
assertSnapshot(of: view.environment(\.colorScheme, .dark),
as: .image(layout: .device(config: .iPhone13)), named: "dark")
// Large Dynamic Type
assertSnapshot(of: view.environment(\.dynamicTypeSize, .accessibility3),
as: .image(layout: .device(config: .iPhone13)), named: "largeText")
}
}Always test Dark Mode and large Dynamic Type in snapshots.
Attach diagnostic data to test results for debugging failures:
@Test func generateReport() async throws {
let report = try generateReport()
// Attach the output for later inspection
Attachment.record(report.data, named: "report.json")
#expect(report.isValid)
}
// Attach from a file URL
@Test func processImage() async throws {
let output = try processImage()
let attachment = try await Attachment(contentsOf: output.url, named: "result.png")
Attachment.record(attachment)
}Attachments support any Attachable type and images via AttachableAsImage.
Test code that calls exit(), fatalError(), or preconditionFailure():
@Test func invalidInputCausesExit() async {
await #expect(processExitsWith: .failure) {
processInvalidInput() // calls fatalError()
}
}Exit testing runs the closure in a subprocess. The test passes if the process exits with the expected status.
Tests/AppTests/ # Swift Testing (Models/, ViewModels/, Services/)
Tests/AppUITests/ # XCTest UI tests (Pages/, Flows/)
Tests/Fixtures/ # Test data (JSON, images)
Tests/Mocks/ # Shared mock implementationsName test files <TypeUnderTest>Tests.swift. Describe behavior in function names: fetchUserReturnsNilOnNetworkError() not testFetchUser(). Name mocks Mock<ProtocolName>.
Always test: business logic, validation rules, state transitions in view models, error handling paths, edge cases (empty collections, nil, boundaries), async success and failure, Task cancellation.
Skip: SwiftUI view body layout (use snapshots), simple property forwarding, Apple framework behavior, private methods (test through public API).
When parameterized test arguments appear in test output, Swift Testing uses String(describing:) by default. Conform to CustomTestStringConvertible for better output:
enum Food: CaseIterable {
case paella, oden, ragu
}
extension Food: CustomTestStringConvertible {
var testDescription: String {
switch self {
case .paella: "paella valenciana"
case .oden: "おでん"
case .ragu: "ragù alla bolognese"
}
}
}
@Test(arguments: Food.allCases)
func isDelicious(_ food: Food) { /* output shows custom descriptions */ }Use this for any type passed as a parameterized test argument where the default description is unclear — especially enums, IDs, or model types.
Use @available on test functions to run tests only on specific OS versions:
@Test
@available(iOS 18, macOS 15, *)
func usesNewAPI() async throws {
let result = try await NewFramework.process()
#expect(result.isValid)
}Swift Testing skips @available-gated tests when running on older OS versions. This replaces XCTest's #available guard + early return pattern.
confirmation with expected counts, not sleep calls.init() in @Suite or a fixture.sleep in tests. Use confirmation, clock injection, or withKnownIssue.Task cancellation, verify it cancels cleanly..serialized as a dependency chain. Serialized scopes avoid overlap; they do not pass state from one test to the next.@Test, #expect), not XCTest assertionsfetchUserReturnsNilOnNetworkError not testFetchUser)confirmation(), not Task.sleep.critical, .slow).serialized is reserved for exclusive state, not workflow sequencingskills
accessorysetupkit
references
activitykit
references
adattributionkit
references
alarmkit
references
app-clips
app-intents
references
app-store-optimization
app-store-review
apple-on-device-ai
appmigrationkit
references
audioaccessorykit
references
authentication
references
avkit
references
background-processing
references
browserenginekit
references
callkit
references
carplay
references
cloudkit
references
contacts-framework
references
core-bluetooth
references
core-data
core-motion
references
core-nfc
references
coreml
references
cryptokit
references
cryptotokenkit
references
debugging-instruments
device-integrity
references
dockkit
references
energykit
references
eventkit
references
financekit
references
focus-engine
gamekit
references
healthkit
references
homekit
references
ios-accessibility
ios-localization
ios-networking
ios-simulator
references
mapkit
metrickit
references
musickit
references
natural-language
references
paperkit
references
passkit
references
pdfkit
references
pencilkit
references
permissionkit
references
photokit
push-notifications
realitykit
references
relevancekit
references
scenekit
references
sensorkit
references
speech-recognition
spritekit
references
storekit
swift-api-design-guidelines
swift-architecture
swift-charts
references
swift-codable
swift-concurrency
swift-formatstyle
swift-language
swift-security
references
swift-testing
swiftdata
swiftlint
swiftui-animation
swiftui-gestures
references
swiftui-layout-components
swiftui-liquid-glass
references
swiftui-patterns
swiftui-performance
swiftui-uikit-interop
swiftui-webkit
tabletopkit
references
tipkit
references
vision-framework
weatherkit
references
widgetkit
references