Install
openclaw skills install swift-xctestSwift 6 + XCTest testing patterns for macOS/iOS apps using SwiftData, EventKit, and SwiftUI
openclaw skills install swift-xctestExpert-level XCTest patterns for Swift 6 macOS/iOS applications. Specialized for SwiftData, EventKit, SwiftUI, and strict concurrency.
Use this skill when:
@MainActor isolationimport SwiftData
import XCTest
@MainActor
final class ModelTests: XCTestCase {
var container: ModelContainer!
var context: ModelContext!
override func setUp() async throws {
try await super.setUp()
// Use in-memory config for isolated tests
let config = ModelConfiguration(isStoredInMemoryOnly: true)
container = try ModelContainer(for: YourModel.self, configurations: config)
context = container.mainContext
}
override func tearDown() async throws {
container = nil
context = nil
try await super.tearDown()
}
}
@MainActor
final class ServiceTests: XCTestCase {
var service: YourActor!
override func setUp() async throws {
try await super.setUp()
service = YourActor()
}
func testActorMethod() async throws {
// Actor-isolated tests work naturally with async
let result = try await service.method()
XCTAssertEqual(result, expected)
}
}
func testFeature() async throws {
// Given: Set up initial state
let task = service.createTask(title: "Test")
// When: Perform action
task.complete()
// Then: Verify result
XCTAssertTrue(task.isCompleted)
}
@MainActor
final class FeatureTests: XCTestCase {
// MARK: - Properties
// Test dependencies
// MARK: - Setup & Teardown
// setUp/tearDown methods
// MARK: - Unit Tests
// Isolated tests
// MARK: - Integration Tests
// Tests across components
// MARK: - Edge Cases
// Boundary and error conditions
// MARK: - Performance Tests
// Performance benchmarks
}
ModelConfiguration(isStoredInMemoryOnly: true)@MainActortry context.save() after mutationsFetchDescriptor and #Predicate@MainActorawait for all actor methodsnonisolated tests where appropriateYourService(inMemory: true)FetchDescriptor queries with predicatesEKEventStore for isolated testingEKReminder to model mapping@Testable import to access internal types@Observable ViewModel behaviorasync/await for all async operations@MainActormeasure blocks for critical paths// MARK: sectionsfunc testPerformance() async throws {
measure {
// Code to measure
_ = service.process(data: largeDataSet)
}
}
func testBatchFetchEfficiency() async throws {
let ids = (0..<100).map { "ID-\($0)" }
let start = Date()
let results = service.fetch(by: ids)
let duration = Date().timeIntervalSince(start)
XCTAssertEqual(results.count, 100)
XCTAssertLessThan(duration, 0.5, "Batch fetch should be fast")
}
func testPredicateFiltering() async throws {
let descriptor = FetchDescriptor<IdentityMap>(
predicate: #Predicate { $0.isDirty == true }
)
let results = try context.fetch(descriptor)
XCTAssertEqual(results.count, expectedCount)
}
# Run all tests
xcodebuild test -scheme YourApp -destination 'platform=macOS'
# Run specific test
xcodebuild test -scheme YourApp -destination 'platform=macOS' \
-only-testing:'YourAppTests/FeatureTests/testMethod'
# Run with performance output
xcodebuild test -scheme YourApp -destination 'platform=macOS' \
-resultBundlePath ~/TestResults.xcresult