swift-testing-pro
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWrite 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.
编写并评审Swift Testing代码,确保其正确性、现代API的合理使用以及对项目规范的遵循。仅报告真实存在的问题——不要吹毛求疵或编造问题。
评审流程:
- 确保测试遵循Swift Testing核心规范,可参考。
references/core-rules.md - 验证测试结构、断言、依赖注入及其他最佳实践,可参考。
references/writing-better-tests.md - 检查异步测试、确认机制、时间限制、Actor隔离和网络模拟,可参考。
references/async-tests.md - 确保新特性(如原始标识符、测试作用域、退出测试和附件)的使用正确,可参考。
references/new-features.md - 如果从XCTest迁移,请遵循中的转换指南。
references/migrating-from-xctest.md
如果仅处理部分工作,只需加载相关的参考文件。
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.
- 目标版本为Swift 6.2或更高版本,使用现代Swift并发模型。
- 作为Swift Testing开发者,用户希望所有新的单元测试和集成测试都使用Swift Testing编写,他们可能会请求帮助将现有XCTest代码迁移至Swift Testing。
- Swift Testing不支持UI测试——UI测试必须使用XCTest。
- 使用一致的项目结构,文件夹布局由应用功能决定。
Swift Testing会随每个Swift版本迭代演进,预计每年发布3到4个版本,每个版本都会引入新特性。这意味着你现有的训练数据可能会自然过时或缺少关键特性。
本技能专门采用最新的Swift和Swift Testing代码,这意味着它会提出一些你可能不了解的建议。请以用户安装的工具链为准,但苹果关于这些API的文档很可能已经过时,因此需谨慎对待。
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
UserTests.swift
Line 5: Use struct, not class, for test suites.
swift
// Before
class UserTests: XCTestCase {
// After
struct UserTests {Line 12: Use instead of .
#expectXCTAssertEqualswift
// Before
XCTAssertEqual(user.name, "Taylor")
// After
#expect(user.name == "Taylor")Line 30: Use for preconditions, not .
#require#expectswift
// Before
#expect(users.isEmpty == false)
let first = users.first!
// After
let first = try #require(users.first)第5行:测试套件应使用struct而非class。
swift
// Before
class UserTests: XCTestCase {
// After
struct UserTests {第12行:使用替代。
#expectXCTAssertEqualswift
// Before
XCTAssertEqual(user.name, "Taylor")
// After
#expect(user.name == "Taylor")第30行:使用处理前置条件,而非。
#require#expectswift
// 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): on line 12 should be migrated to
XCTAssertEqual.#expect - Assertions (medium): Force-unwrap on line 30 should use to unwrap safely and stop the test early on failure.
#require
End of example.
- 基础规范(高优先级): 第5行的测试套件应改为struct,而非继承自的class。
XCTestCase - 迁移(中优先级): 第12行的应迁移至
XCTAssertEqual。#expect - 断言(中优先级): 第30行的强制解包应使用安全解包,并在测试失败时提前终止。
#require
示例结束。
References
参考资料
- - core Swift Testing rules: structs over classes,
references/core-rules.md/initover setUp/tearDown, parallel execution, parameterized tests,deinit, and tags.withKnownIssue - - test hygiene, structuring tests, hidden dependencies,
references/writing-better-tests.mdvs#expect,#require,Issue.record(), and verification methods.#expect(throws:) - - serialized tests,
references/async-tests.md, time limits, actor isolation, testing pre-concurrency code, and mocking networking.confirmation() - - raw identifiers, range-based confirmations, test scoping traits, exit tests, attachments,
references/new-features.md, and the updatedConditionTrait.evaluate()return value.#expect(throws:) - - XCTest-to-Swift Testing conversion steps, assertion mappings, and floating-point tolerance via Swift Numerics.
references/migrating-from-xctest.md
- - Swift Testing核心规则:优先使用struct而非class,用
references/core-rules.md/init替代setUp/tearDown,并行执行,参数化测试,deinit和标签。withKnownIssue - - 测试卫生、测试结构、隐藏依赖、
references/writing-better-tests.md与#expect对比、#require、Issue.record()和验证方法。#expect(throws:) - - 序列化测试、
references/async-tests.md、时间限制、Actor隔离、并发前代码测试和网络模拟。confirmation() - - 原始标识符、基于范围的确认、测试作用域特性、退出测试、附件、
references/new-features.md以及更新后的ConditionTrait.evaluate()返回值。#expect(throws:) - - XCTest到Swift Testing的转换步骤、断言映射,以及通过Swift Numerics实现的浮点容差。
references/migrating-from-xctest.md