← 返回 Skills 市场
soponcd

Swift Xctest

作者 soponcd · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
137
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install swift-xctest
功能描述
Swift 6 + XCTest testing patterns for macOS/iOS apps using SwiftData, EventKit, and SwiftUI
使用说明 (SKILL.md)

Swift/XCTest Testing Skill

Expert-level XCTest patterns for Swift 6 macOS/iOS applications. Specialized for SwiftData, EventKit, SwiftUI, and strict concurrency.

When to Use

Use this skill when:

  • Writing XCTest for Swift 6 applications
  • Testing SwiftData models with in-memory containers
  • Testing Swift 6 actors and @MainActor isolation
  • Testing EventKit integrations
  • Testing SwiftUI views and @Observable ViewModels
  • Writing performance tests for macOS apps

Core Principles

1. SwiftData Testing Pattern

import 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()
    }
}

2. Swift 6 Actor Testing

@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)
    }
}

3. Given-When-Then Pattern

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)
}

4. Test Organization

@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
}

Testing Guidelines

SwiftData Models

  • Always use ModelConfiguration(isStoredInMemoryOnly: true)
  • Mark test classes with @MainActor
  • Use try context.save() after mutations
  • Verify state with FetchDescriptor and #Predicate

Actors & Concurrency

  • Test classes interacting with actors should be @MainActor
  • Use await for all actor methods
  • Test isolation boundaries with nonisolated tests where appropriate

SwiftData Services

  • Use in-memory mode: YourService(inMemory: true)
  • Test fetch, create, update, delete operations
  • Verify FetchDescriptor queries with predicates

EventKit Mocking

  • Mock EKEventStore for isolated testing
  • Test permission handling scenarios
  • Verify EKReminder to model mapping

SwiftUI Views

  • Use @Testable import to access internal types
  • Test @Observable ViewModel behavior
  • Test state transitions and side effects

Best Practices

  1. Isolation: Each test should be independent
  2. Async-Safety: Use async/await for all async operations
  3. MainActor: Mark SwiftData tests with @MainActor
  4. Performance: Use measure blocks for critical paths
  5. Readability: Use Given-When-Then comments
  6. Organization: Group tests with // MARK: sections

Performance Testing

func testPerformance() async throws {
    measure {
        // Code to measure
        _ = service.process(data: largeDataSet)
    }
}

Common Test Patterns

Batch Query Testing

func testBatchFetchEfficiency() async throws {
    let ids = (0..\x3C100).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")
}

Predicate Testing

func testPredicateFiltering() async throws {
    let descriptor = FetchDescriptor\x3CIdentityMap>(
        predicate: #Predicate { $0.isDirty == true }
    )
    let results = try context.fetch(descriptor)
    XCTAssertEqual(results.count, expectedCount)
}

Running Tests

# 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
安全使用建议
This skill is an instructional guide for writing XCTest tests and appears coherent. Before using it: (1) understand that running the provided xcodebuild commands executes the project's test code — only run tests for code you trust or in an isolated environment (container/VM). (2) Ensure you have Xcode and required SDKs installed; the skill assumes xcodebuild and project schemes exist. (3) Review any third‑party test code you copy into your repo for network calls, shell execution, or other side effects. If you want a deeper review, provide the actual test files or the project the tests will run against so I can check for runtime actions (networking, file I/O, spawned processes).
功能分析
Type: OpenClaw Skill Name: swift-xctest Version: 1.0.0 The skill bundle provides standard Swift 6 and XCTest patterns for iOS/macOS development, focusing on SwiftData, EventKit, and SwiftUI. The content consists entirely of educational documentation and boilerplate code snippets for unit testing, with no evidence of malicious intent, data exfiltration, or prompt injection.
能力评估
Purpose & Capability
The name/description (Swift + XCTest patterns) matches the SKILL.md contents. Examples and run instructions (xcodebuild) are directly related to writing and running tests for Swift/iOS/macOS apps.
Instruction Scope
SKILL.md contains only test patterns, code snippets, and xcodebuild commands — all within the stated purpose. Note: the instructions include running xcodebuild which runs the project's test code (i.e., arbitrary code in the repository under test), so executing those commands on untrusted projects can execute arbitrary code on the host.
Install Mechanism
No install spec and no code files are present (instruction-only). Nothing is downloaded or written to disk by the skill itself.
Credentials
No environment variables, credentials, or config paths are requested. The skill's requirements are minimal and appropriate for its stated purpose.
Persistence & Privilege
always is false and the skill does not request persistent system presence or modify other skills/config. Autonomous invocation is permitted by platform default but is not elevated here.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install swift-xctest
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /swift-xctest 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release of swift-xctest skill. - Provides expert-level testing patterns for Swift 6 macOS/iOS apps using XCTest. - Includes guidance for testing SwiftData, EventKit integrations, SwiftUI views, and actor-based concurrency. - Offers core testing patterns, best practices, and code snippets for setup, isolation, and performance benchmarking. - Documents recommended test organization and usage of in-memory models, mocking, and the Given-When-Then approach. - Contains sample commands for running and measuring tests with xcodebuild.
元数据
Slug swift-xctest
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

Swift Xctest 是什么?

Swift 6 + XCTest testing patterns for macOS/iOS apps using SwiftData, EventKit, and SwiftUI. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 137 次。

如何安装 Swift Xctest?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install swift-xctest」即可一键安装,无需额外配置。

Swift Xctest 是免费的吗?

是的,Swift Xctest 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Swift Xctest 支持哪些平台?

Swift Xctest 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Swift Xctest?

由 soponcd(@soponcd)开发并维护,当前版本 v1.0.0。

💬 留言讨论