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

swift-testingSwift 测试

Agent Skill

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

总安装

29,376

周安装

1,221

GitHub Stars

517

下载量

9,504
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill swift-testing

简介

现代 Swift 测试框架,具有 @Test 宏、参数化测试、特征和异步支持。

  • 使用@Test编写单元测试
  • 带有 #expect 的宏
  • 和#require
  • 断言;对于所有新测试,更喜欢 Swift 测试而不是 XCTest(Xcode 16+、Swift 6+)
  • 使用@Suite组织测试
  • 用于分组、用于过滤的自定义标签以及用于条件执行、时间限制和已知问题的特征
  • 通过参数支持参数化测试:
  • 具有枚举情况、范围或笛卡尔积;使用确认()
  • 在不睡眠的情况下验证异步回调
  • 本机处理异步/等待模式,使用抛出测试错误路径:
  • 期望,并使用等待管理参与者隔离状态

SKILL.md

Swift Testing

Swift Testing is the modern testing framework for Swift (Xcode 16+, Swift 6+). Prefer it over XCTest for all new unit tests. Use XCTest only for UI tests, performance benchmarks, and snapshot tests.

Contents


Basic Tests

import Testing

@Test("User can update their display name")
func updateDisplayName() {
    var user = User(name: "Alice")
    user.name = "Bob"
    #expect(user.name == "Bob")
}

@Test Traits

@Test("Validates email format")                                    // display name
@Test(.tags(.validation, .email))                                  // tags
@Test(.disabled("Server migration in progress"))                   // disabled
@Test(.enabled(if: ProcessInfo.processInfo.environment["CI"] != nil)) // conditional
@Test(.bug("https://github.com/org/repo/issues/42"))               // bug reference
@Test(.timeLimit(.minutes(1)))                                     // time limit
@Test("Timeout handling", .tags(.networking), .timeLimit(.seconds(30))) // combined

#expect and #require

// #expect records failure but continues execution
#expect(result == 42)
#expect(name.isEmpty == false)
#expect(items.count > 0, "Items should not be empty")

// #expect with error type checking
#expect(throws: ValidationError.self) {
    try validate(email: "not-an-email")
}

// #expect with specific error value
#expect {
    try validate(email: "")
} throws: { error in
    guard let err = error as? ValidationError else { return false }
    return err == .empty
}

// #require records failure AND stops test (like XCTUnwrap)
let user = try #require(await fetchUser(id: 1))
#expect(user.name == "Alice")

// #require for optionals -- unwraps or fails
let first = try #require(items.first)
#expect(first.isValid)

Rule: Use #require when subsequent assertions depend on the value. Use #expect for independent checks.

@Suite and Test Organization

See references/testing-patterns.md for suite organization, confirmation patterns, known-issue handling, and execution-model details.

Execution Model

Swift Testing runs tests in parallel by default. Do not assume test order, shared suite instances, or exclusive access to mutable state unless you explicitly design for it.

@Suite(.serialized)
struct KeychainTests {
    @Test func storesToken() throws { /* ... */ }
    @Test func deletesToken() throws { /* ... */ }
}

Use .serialized when a test or suite must run one-at-a-time because it touches shared external state. It does not make unrelated tests outside that scope run serially.

Rules:

  • Each test must set up its own state.
  • Shared mutable globals are a bug unless protected or intentionally serialized.
  • @Suite(.serialized) is for exclusive execution, not for expressing logical ordering between tests.
  • If tests depend on sequence, combine them into one test or move the sequence into shared helper code.

Known Issues

Mark expected failures so they do not cause test failure:

withKnownIssue("Propane tank is empty") {
    #expect(truck.grill.isHeating)
}

// Intermittent / flaky failures
withKnownIssue(isIntermittent: true) {
    #expect(service.isReachable)
}

// Conditional known issue
withKnownIssue {
    #expect(foodTruck.grill.isHeating)
} when: {
    !hasPropane
}

If no known issues are recorded, Swift Testing records a distinct issue notifying you the problem may be resolved.

Additional Patterns

See references/testing-patterns.md for parameterized tests, tags and suites, async testing, traits, and execution-model details.

Test Attachments

Attach diagnostic data to test results for debugging failures. See references/testing-patterns.md for full examples.

@Test func generateReport() async throws {
    let report = try generateReport()
    Attachment.record(report.data, named: "report.json")
    #expect(report.isValid)
}

Image attachments are available via cross-import overlays — import both Testing and a UI framework:

import Testing
import UIKit

@Test func renderedChart() async throws {
    let image = renderer.image { ctx in chartView.drawHierarchy(in: bounds, afterScreenUpdates: true) }
    Attachment.record(image, named: "chart.png")
}

Exit Testing

Test code that calls exit(), fatalError(), or preconditionFailure(). See references/testing-patterns.md for details.

@Test func invalidInputCausesExit() async {
    await #expect(processExitsWith: .failure) {
        processInvalidInput()  // calls fatalError()
    }
}

Common Mistakes

  1. Testing implementation, not behavior. Test what the code does, not how.
  2. No error path tests. If a function can throw, test the throw path.
  3. Flaky async tests. Use confirmation with expected counts, not sleep calls.
  4. Shared mutable state between tests. Each test sets up its own state via init() in @Suite.
  5. Missing accessibility identifiers in UI tests. XCUITest queries rely on them.
  6. Using sleep in tests. Use confirmation, clock injection, or withKnownIssue.
  7. Not testing cancellation. If code supports Task cancellation, verify it cancels cleanly.
  8. Mixing XCTest and Swift Testing in one file. Keep them in separate files.
  9. Non-Sendable test helpers shared across tests. Ensure test helper types are Sendable when shared across concurrent test cases. Annotate MainActor-dependent test code with @MainActor.
  10. Assuming tests run in declaration order. Swift Testing runs in parallel by default; use .serialized only when exclusive execution is required.
  11. Using .serialized to express workflow steps. Serialized execution does not make one test feed another; keep dependent steps in one test.

Review Checklist

  • All new tests use Swift Testing (@Test, #expect), not XCTest assertions
  • Test names describe behavior (fetchUserReturnsNilOnNetworkError not testFetchUser)
  • Error paths have dedicated tests
  • Async tests use confirmation(), not Task.sleep
  • Parameterized tests used for repetitive variations
  • Tags applied for filtering (.critical, .slow)
  • Mocks conform to protocols, not subclass concrete types
  • No shared mutable state between tests
  • Tests do not rely on declaration order or shared suite instances
  • .serialized used only for truly exclusive state, not to model workflow sequencing
  • Cancellation tested for cancellable async operations

References

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

35.96%
按下载量换算3,418

Claude

28.28%
按下载量换算2,688

Cursor

16.95%
按下载量换算1,611

Gemini CLI

9.07%
按下载量换算862

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

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

安装前确认

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

来源信息

继续浏览同类 Skills