swift-testing-pro

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
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:
  1. Ensure tests follow core Swift Testing conventions using
    references/core-rules.md
    .
  2. Validate test structure, assertions, dependency injection, and other best practices using
    references/writing-better-tests.md
    .
  3. Check async tests, confirmations, time limits, actor isolation, and networking mocks using
    references/async-tests.md
    .
  4. Ensure new features like raw identifiers, test scopes, exit tests, and attachments are used correctly using
    references/new-features.md
    .
  5. 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的合理使用以及对项目规范的遵循。仅报告真实存在的问题——不要吹毛求疵或编造问题。
评审流程:
  1. 确保测试遵循Swift Testing核心规范,可参考
    references/core-rules.md
  2. 验证测试结构、断言、依赖注入及其他最佳实践,可参考
    references/writing-better-tests.md
  3. 检查异步测试、确认机制、时间限制、Actor隔离和网络模拟,可参考
    references/async-tests.md
  4. 确保新特性(如原始标识符、测试作用域、退出测试和附件)的使用正确,可参考
    references/new-features.md
  5. 如果从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:
  1. State the file and relevant line(s).
  2. Name the rule being violated.
  3. 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:
如果用户请求评审,请按文件整理发现的问题。对于每个问题:
  1. 说明文件及相关行号。
  2. 指出违反的规则名称。
  3. 展示简短的修复前后代码对比。
跳过没有问题的文件。最后给出按优先级排序的最具影响力变更总结。
如果用户请求编写或改进测试,请遵循上述相同规则,但直接进行修改,而非返回问题报告。
示例输出:

UserTests.swift

UserTests.swift

Line 5: Use struct, not class, for test suites.
swift
// Before
class UserTests: XCTestCase {

// After
struct UserTests {
Line 12: Use
#expect
instead of
XCTAssertEqual
.
swift
// Before
XCTAssertEqual(user.name, "Taylor")

// After
#expect(user.name == "Taylor")
Line 30: Use
#require
for preconditions, not
#expect
.
swift
// 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行:使用
#expect
替代
XCTAssertEqual
swift
// Before
XCTAssertEqual(user.name, "Taylor")

// After
#expect(user.name == "Taylor")
第30行:使用
#require
处理前置条件,而非
#expect
swift
// Before
#expect(users.isEmpty == false)
let first = users.first!

// After
let first = try #require(users.first)

Summary

总结

  1. Fundamentals (high): Test suite on line 5 should be a struct, not a class inheriting from
    XCTestCase
    .
  2. Migration (medium):
    XCTAssertEqual
    on line 12 should be migrated to
    #expect
    .
  3. Assertions (medium): Force-unwrap on line 30 should use
    #require
    to unwrap safely and stop the test early on failure.
End of example.
  1. 基础规范(高优先级): 第5行的测试套件应改为struct,而非继承自
    XCTestCase
    的class。
  2. 迁移(中优先级): 第12行的
    XCTAssertEqual
    应迁移至
    #expect
  3. 断言(中优先级): 第30行的强制解包应使用
    #require
    安全解包,并在测试失败时提前终止。
示例结束。

References

参考资料

  • references/core-rules.md
    - core Swift Testing rules: structs over classes,
    init
    /
    deinit
    over setUp/tearDown, parallel execution, parameterized tests,
    withKnownIssue
    , and tags.
  • references/writing-better-tests.md
    - test hygiene, structuring tests, hidden dependencies,
    #expect
    vs
    #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.
  • references/core-rules.md
    - Swift Testing核心规则:优先使用struct而非class,用
    init
    /
    deinit
    替代setUp/tearDown,并行执行,参数化测试,
    withKnownIssue
    和标签。
  • references/writing-better-tests.md
    - 测试卫生、测试结构、隐藏依赖、
    #expect
    #require
    对比、
    Issue.record()
    #expect(throws:)
    和验证方法。
  • references/async-tests.md
    - 序列化测试、
    confirmation()
    、时间限制、Actor隔离、并发前代码测试和网络模拟。
  • references/new-features.md
    - 原始标识符、基于范围的确认、测试作用域特性、退出测试、附件、
    ConditionTrait.evaluate()
    以及更新后的
    #expect(throws:)
    返回值。
  • references/migrating-from-xctest.md
    - XCTest到Swift Testing的转换步骤、断言映射,以及通过Swift Numerics实现的浮点容差。