Token导航 LogoToken导航TokenDH.com
前端设计需要联网github未标认证来源可访问许可证需确认审计通过

swift-testingSwift 测试

Agent Skill

用于辅助测试设计、自动化测试、用例整理和回归验证。它适合让 Agent 编写单元测试、端到端测试、测试计划或根据失败日志定位问题。使用时需要确认项目测试框架、运行命令和夹具数据,避免为了通过测试而改坏真实逻辑;涉及浏览器或外部服务时,应区分本地模拟、测试环境和生产环境。

总安装

2,928

周安装

122

GitHub Stars

72

下载量

976
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:swift-testing(Swift 测试)
来源仓库:https://github.com/bocato/swift-testing-agent-skill
仓库路径:skills/swift-testing
安装命令:
npx skills add https://github.com/bocato/swift-testing-agent-skill --skill swift-testing
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/bocato/swift-testing-agent-skill --skill swift-testing

简介

swift-testing 用于辅助 Swift 项目的测试设计与实现,适合在 Codex、Claude、Cursor、Gemini CLI 中编写单元测试、端到端测试或分析测试问题时使用。

  • 它基于 Swift Testing 框架,遵循 Arrange-Act-Assert 模式和 F.I.R.S.T. 原则,支持测试夹具、快照测试和迁移指导。
  • 使用时需确认项目是否已集成 Swift Testing,并配合本地构建和测试命令验证结果。
  • 涉及浏览器或外部服务时,应区分本地模拟、测试环境和生产环境,避免误改真实逻辑。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Swift Testing

Overview

This skill provides expert guidance on Swift Testing, covering the modern Swift Testing framework, test doubles (mocks, stubs, spies), fixtures, integration testing, snapshot testing, and migration from XCTest. Use this skill to help developers write reliable, maintainable tests following F.I.R.S.T. principles and Arrange-Act-Assert patterns.

Agent Behavior Contract (Follow These Rules)

  1. Use Swift Testing framework (@Test, #expect, #require, @Suite) for all new tests, not XCTest.
  2. Always structure tests with clear Arrange-Act-Assert phases.
  3. Follow F.I.R.S.T. principles: Fast, Isolated, Repeatable, Self-Validating, Timely.
  4. Use proper test double terminology per Martin Fowler's taxonomy (Dummy, Fake, Stub, Spy, SpyingStub, Mock).
  5. Place fixtures close to models with #if DEBUG, not in test targets.
  6. Place test doubles close to interfaces with #if DEBUG, not in test targets.
  7. Prefer state verification over behavior verification - simpler, less brittle tests.
  8. Use #expect for soft assertions (continue on failure) and #require for hard assertions (stop on failure).

Quick Decision Tree

When a developer needs testing guidance, follow this decision tree:

  1. Starting fresh with Swift Testing?

- Read references/test-organization.md for suites, tags, traits - Read references/async-testing.md for async test patterns

  1. Need to create test data?

- Read references/fixtures.md for fixture patterns and placement - Read references/test-doubles.md for mock/stub/spy patterns

  1. Testing multiple inputs?

- Read references/parameterized-tests.md for parameterized testing

  1. Testing module interactions?

- Read references/integration-testing.md for integration test patterns

  1. Testing UI for regressions?

- Read references/snapshot-testing.md for snapshot testing setup

  1. Testing data structures or state?

- Read references/dump-snapshot-testing.md for text-based snapshot testing

  1. Migrating from XCTest?

- Read references/migration-xctest.md for migration guide

Triage-First Playbook (Common Errors -> Next Best Move)

  • "XCTAssertEqual is unavailable" / need to modernize tests

- Use references/migration-xctest.md for XCTest to Swift Testing migration

  • Need to test async code

- Use references/async-testing.md for async patterns, confirmation, timeouts

  • Tests are slow or flaky

- Check F.I.R.S.T. principles, use proper mocking per references/test-doubles.md

  • Need deterministic test data

- Use references/fixtures.md for fixture patterns with fixed dates

  • Need to test multiple scenarios efficiently

- Use references/parameterized-tests.md for parameterized testing

  • Need to verify component interactions

- Use references/integration-testing.md for integration test patterns

Core Syntax

Basic Test

import Testing

@Test func basicTest() {
    #expect(1 + 1 == 2)
}

Test with Description

@Test("Adding items increases cart count")
func addItem() {
    let cart = Cart()
    cart.add(item)
    #expect(cart.count == 1)
}

Async Test

@Test func asyncOperation() async throws {
    let result = try await service.fetch()
    #expect(result.isValid)
}

Arrange-Act-Assert Pattern

Structure every test with clear phases:

@Test func calculateTotal() {
    // Given
    let cart = ShoppingCart()
    cart.add(Item(price: 10))
    cart.add(Item(price: 20))

    // When
    let total = cart.calculateTotal()

    // Then
    #expect(total == 30)
}

Assertions

#expect - Soft Assertion

Continues test execution after failure:

@Test func multipleExpectations() {
    let user = User(name: "Alice", age: 30)
    #expect(user.name == "Alice")  // If fails, test continues
    #expect(user.age == 30)        // This still runs
}

#require - Hard Assertion

Stops test execution on failure:

@Test func requireExample() throws {
    let user = try #require(fetchUser())  // Stops if nil
    #expect(user.name == "Alice")
}

Error Testing

@Test func throwsError() {
    #expect(throws: ValidationError.self) {
        try validate(invalidInput)
    }
}

@Test func throwsSpecificError() {
    #expect(throws: ValidationError.emptyField) {
        try validate("")
    }
}

F.I.R.S.T. Principles

PrincipleDescriptionApplication
FastTests execute in millisecondsMock expensive operations
IsolatedTests don't depend on each otherFresh instance per test
RepeatableSame result every timeMock dates, network, external deps
Self-ValidatingAuto-report pass/failUse #expect, never rely on print()
TimelyWrite tests alongside codeUse parameterized tests for edge cases

Test Double Quick Reference

Per Martin Fowler's definition:

TypePurposeVerification
DummyFill parameters, never usedN/A
FakeWorking implementation with shortcutsState
StubProvides canned answersState
SpyRecords calls for verificationState
SpyingStubStub + Spy combined (most common)State
MockPre-programmed expectations, self-verifiesBehavior

Important: What Swift community calls "Mock" is usually a SpyingStub.

For detailed patterns, see references/test-doubles.md.

Test Double Placement

Place test doubles close to the interface, not in test targets:

// In PersonalRecordsCore-Interface/Sources/...

public protocol PersonalRecordsRepositoryProtocol: Sendable {
    func getAll() async throws -> [PersonalRecord]
    func save(_ record: PersonalRecord) async throws
}

#if DEBUG
public final class PersonalRecordsRepositorySpyingStub: PersonalRecordsRepositoryProtocol {
    // Spy: Captured calls
    public private(set) var savedRecords: [PersonalRecord] = []

    // Stub: Configurable responses
    public var recordsToReturn: [PersonalRecord] = []
    public var errorToThrow: Error?

    public func getAll() async throws -> [PersonalRecord] {
        if let error = errorToThrow { throw error }
        return recordsToReturn
    }

    public func save(_ record: PersonalRecord) async throws {
        if let error = errorToThrow { throw error }
        savedRecords.append(record)
    }
}
#endif

Fixtures

Place fixtures close to the model:

// In Sources/Models/PersonalRecord.swift

public struct PersonalRecord: Equatable, Sendable {
    public let id: UUID
    public let weight: Double
    // ...
}

#if DEBUG
extension PersonalRecord {
    public static func fixture(
        id: UUID = UUID(),
        weight: Double = 100.0
        // ... defaults for all properties
    ) -> PersonalRecord {
        PersonalRecord(id: id, weight: weight)
    }
}
#endif

For detailed patterns, see references/fixtures.md.

Test Pyramid

        +-------------+
        |   UI Tests  |  5%  - End-to-end flows
        |   (E2E)     |
        +-------------+
        | Integration |  15% - Module interactions
        |    Tests    |
        +-------------+
        |    Unit     |  80% - Individual components
        |    Tests    |
        +-------------+

Reference Files

Load these files as needed for specific topics:

  • test-organization.md - Suites, tags, traits, parallel execution
  • parameterized-tests.md - Testing multiple inputs efficiently
  • async-testing.md - Async patterns, confirmation, timeouts, cancellation
  • migration-xctest.md - Complete XCTest to Swift Testing migration guide
  • test-doubles.md - Complete taxonomy with examples (Dummy, Fake, Stub, Spy, SpyingStub, Mock)
  • fixtures.md - Fixture patterns, placement, and best practices
  • integration-testing.md - Module interaction testing patterns
  • snapshot-testing.md - UI regression testing with SnapshotTesting library
  • dump-snapshot-testing.md - Text-based snapshot testing for data structures

Best Practices Summary

  1. Use Swift Testing for new tests - Modern syntax, better features
  2. Follow Arrange-Act-Assert - Clear test structure
  3. Apply F.I.R.S.T. principles - Fast, Isolated, Repeatable, Self-Validating, Timely
  4. Place fixtures near models - With #if DEBUG guards
  5. Place test doubles near interfaces - With #if DEBUG guards
  6. Prefer state verification - Simpler, less brittle than behavior verification
  7. Use parameterized tests - For testing multiple inputs efficiently
  8. Follow test pyramid - 80% unit, 15% integration, 5% UI

Verification Checklist (When You Write Tests)

  • Tests follow Arrange-Act-Assert pattern
  • Test names describe behavior, not implementation
  • Fixtures use sensible defaults, not random values
  • Test doubles are minimal (only stub what's needed)
  • Async tests use proper patterns (async/await, confirmation)
  • Tests are fast (mock expensive operations)
  • Tests are isolated (no shared state)
  • Tests are repeatable (no flaky date/time dependencies)

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

35.01%
按下载量换算342

Claude

30.08%
按下载量换算294

Cursor

16.29%
按下载量换算159

Gemini CLI

9.32%
按下载量换算91

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills