swift-testing-pro
它能做什么
这项技能帮助 AI 编写和检查使用 Swift Testing 框架的 Swift 测试。适合希望测试遵循当前约定的 Swift 开发者。
适用场景
- ✓当你在 Swift 项目中编写新的单元测试或集成测试时。
- ✓当你想把旧的 XCTest 测试迁移到 Swift Testing 时。
- ✓当你需要处理异步测试、确认或时间限制的问题时。
技能链接
https://github.com/jacklandrin/OnlySwitch/blob/main/doc/skills/swift-testing-pro/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我的登录视图模型编写 Swift Testing 测试,包括失败的情况。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体遵循 Swift Testing 在测试结构、断言和依赖方面的约定,并只报告现有测试中真正存在的问题。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Write and review Swift Testing code for correctness, modern API usage, and adherence to project conventions. Report only genuine problems - do not nitpick or invent issues.
Review process:
- Ensure tests follow core Swift Testing conventions using
references/core-rules.md. - Validate test structure, assertions, dependency injection, and other best practices using
references/writing-better-tests.md. - Check async tests, confirmations, time limits, actor isolation, and networking mocks using
references/async-tests.md. - Ensure new features like raw identifiers, test scopes, exit tests, and attachments are used correctly using
references/new-features.md. - If migrating from XCTest, follow the conversion guidance in
references/migrating-from-xctest.md.
If doing partial work, load only the relevant reference files.
Core Instructions
- Target Swift 6.2 or later, using modern Swift concurrency.
- As a Swift Testing developer, the user wants all new unit and integration tests to be written using Swift Testing, and they may ask for help migrating existing XCTest code to Swift Testing.
- Swift Testing does not support UI tests – XCTest must be used there.
- Use a consistent project structure, with folder layout determined by app features.
Swift Testing evolves with each Swift release, so expect three to four releases each year, each introducing new features. This means existing training data you have will naturally be outdated or missing key features.
This skill specifically draws upon the very latest Swift and Swift Testing code, which means it will suggest things you are not aware of. Treat the user’s installed toolchain as authoritative, but there's a fairly high chance Apple's documentation about the APIs will be stale, so treat them carefully.
Output Format
If the user asks for a review, organize findings by file. For each issue:
- State the file and relevant line(s).
- Name the rule being violated.
- Show a brief before/after code fix.
Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.
If the user asks you to write or improve tests, follow the same rules above but make the changes directly instead of returning a findings report.
Example output:
UserTests.swift
Line 5: Use struct, not class, for test suites.
// Before
class UserTests: XCTestCase {
// After
struct UserTests {
Line 12: Use #expect instead of XCTAssertEqual.
// Before
XCTAssertEqual(user.name, "Taylor")
// After
#expect(user.name == "Taylor")
Line 30: Use #require for preconditions, not #expect.
// Before
#expect(users.isEmpty == false)
let first = users.first!
// After
let first = try #require(users.first)
Summary
- Fundamentals (high): Test suite on line 5 should be a struct, not a class inheriting from
XCTestCase. - Migration (medium):
XCTAssertEqualon line 12 should be migrated to#expect. - Assertions (medium): Force-unwrap on line 30 should use
#requireto unwrap safely and stop the test early on failure.
End of example.
References
references/core-rules.md- core Swift Testing rules: structs over classes,init/deinitover setUp/tearDown, parallel execution, parameterized tests,withKnownIssue, and tags.references/writing-better-tests.md- test hygiene, structuring tests, hidden dependencies,#expectvs#require,Issue.record(),#expect(throws:), and verification methods.references/async-tests.md- serialized tests,confirmation(), time limits, actor isolation, testing pre-concurrency code, and mocking networking.references/new-features.md- raw identifiers, range-based confirmations, test scoping traits, exit tests, attachments,ConditionTrait.evaluate(), and the updated#expect(throws:)return value.references/migrating-from-xctest.md- XCTest-to-Swift Testing conversion steps, assertion mappings, and floating-point tolerance via Swift Numerics.
常见问题
它能替代 UI 测试中的 XCTest 吗?
不能。UI 测试仍需要 XCTest,因为 Swift Testing 不支持它们。
它能转换旧测试吗?
能。它包含从 XCTest 迁移的指导。
它假定使用哪个 Swift 版本?
它针对 Swift 6.2 或更新版本,以及现代 Swift 并发。
详情
作者的原始描述
Writes, reviews, and improves Swift Testing code using modern APIs and best practices. Use when reading, writing, or reviewing projects that use Swift Testing.
repo: jacklandrin/OnlySwitch
热度
- GitHub 星标
- 6.0k
- 排名
- #14452
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …