cmux-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

cmux Testing

cmux测试

Test framework

测试框架

Swift Testing is the current Apple-supported primitive for tests on this codebase (shipped with Swift 6 / Xcode 16, supported on the macOS versions we target). Use it for everything that is not a UI test.
  • Default to Swift Testing for all unit and integration tests.
    import Testing
    , annotate tests with
    @Test
    , group with
    @Suite
    , assert with
    #expect(...)
    and
    try #require(...)
    . Do not write new tests with
    import XCTest
    unless they are UI tests.
  • UI tests stay on XCTest / XCUITest. Swift Testing does not support UI testing (no
    XCUIApplication
    integration). Files under
    cmuxUITests/
    continue to use
    XCTestCase
    +
    XCUIApplication
    . Do not migrate them and do not try to bridge Swift Testing into UI tests.
  • New test targets start on Swift Testing. Every new Swift package's
    Tests/<Name>Tests/
    directory (e.g.
    Packages/CmuxSettings/Tests/CmuxSettingsTests/
    ) should ship with Swift Testing from the first commit. Xcode 16 auto-detects the framework based on the
    import Testing
    statement; no extra
    Package.swift
    configuration is required.
  • Migration guide when touching an existing XCTest test. Convert in place:
    XCTestCase
    subclass becomes a
    @Suite struct
    (or
    final class
    if you need a reference type); each
    func testFoo()
    becomes
    @Test func foo()
    ;
    XCTAssertEqual(a, b)
    becomes
    #expect(a == b)
    ;
    XCTAssertTrue(cond)
    becomes
    #expect(cond)
    ;
    XCTUnwrap(x)
    becomes
    try #require(x)
    ;
    XCTFail("msg")
    becomes
    Issue.record("msg")
    .
    setUp()
    becomes
    init()
    on the suite;
    tearDown()
    becomes
    deinit
    . Async setup is
    async init()
    . Do not bulk-rewrite untouched tests; migrate incrementally as a side effect of editing the file.
  • Parameterized tests use
    @Test(arguments: [...])
    . Prefer this over duplicate test methods.
  • Parallelization and shared state. Swift Testing runs tests in parallel by default, including across suites. If a suite genuinely needs ordering or guards shared mutable state, annotate it with
    .serialized
    instead of adding locks or sleeps.
  • Tags with
    @Test(.tags(.something))
    (or on a
    @Suite
    ) let CI and local runs filter selectively.
Swift Testing是当前Apple支持的针对此代码库的测试基础组件(随Swift 6 / Xcode 16发布,支持我们目标的macOS版本)。所有非UI测试都应使用它。
  • 所有单元测试和集成测试默认使用Swift Testing。 导入
    Testing
    ,用
    @Test
    标注测试,用
    @Suite
    进行分组,用
    #expect(...)
    try #require(...)
    进行断言。除非是UI测试,否则不要用
    import XCTest
    编写新测试。
  • UI测试仍使用XCTest / XCUITest。 Swift Testing不支持UI测试(无
    XCUIApplication
    集成)。
    cmuxUITests/
    下的文件继续使用
    XCTestCase
    +
    XCUIApplication
    。不要迁移这些测试,也不要尝试将Swift Testing桥接到UI测试中。
  • 新测试目标从Swift Testing开始。 每个新Swift包的
    Tests/<Name>Tests/
    目录(例如
    Packages/CmuxSettings/Tests/CmuxSettingsTests/
    )应从第一次提交起就使用Swift Testing。Xcode 16会根据
    import Testing
    语句自动检测框架;无需额外的
    Package.swift
    配置。
  • 修改现有XCTest测试时的迁移指南。 就地转换:
    XCTestCase
    子类变为
    @Suite struct
    (如果需要引用类型则为
    final class
    );每个
    func testFoo()
    变为
    @Test func foo()
    XCTAssertEqual(a, b)
    变为
    #expect(a == b)
    XCTAssertTrue(cond)
    变为
    #expect(cond)
    XCTUnwrap(x)
    变为
    try #require(x)
    XCTFail("msg")
    变为
    Issue.record("msg")
    setUp()
    变为套件的
    init()
    tearDown()
    变为
    deinit
    。异步初始化使用
    async init()
    。不要批量重写未改动的测试;在编辑文件时作为副作用逐步迁移。
  • 参数化测试使用
    @Test(arguments: [...])
    。优先使用此方式而非重复测试方法。
  • 并行化与共享状态。 Swift Testing默认并行运行测试,包括跨套件运行。如果某个套件确实需要有序执行或保护共享可变状态,请用
    .serialized
    标注它,而非添加锁或睡眠。
  • 标签使用
    @Test(.tags(.something))
    (或在
    @Suite
    上),让CI和本地运行可以选择性过滤测试。

Test target validation

测试目标验证

reload.sh
does not compile the test target. It builds only the
cmux
scheme, so a green
reload.sh
says nothing about whether
cmuxTests
/
cmuxUITests
still compile. A symbol that is moved or renamed can keep the
cmux
app building while breaking the test target (real case: a
write(to:atomically:)
typo and a removed
TabManager.CommandResult
only surfaced in the
tests
job). Before pushing package/refactor changes, build the
cmux-unit
scheme (with
-derivedDataPath /tmp/cmux-<tag>
and, for
cmuxApp
/
AppDelegate
churn, the GlobalISel workaround flag) or let the
tests
CI job gate it — never treat
reload.sh
alone as proof the tests build.
reload.sh
不会编译测试目标。它仅构建
cmux
scheme,因此
reload.sh
执行成功并不代表
cmuxTests
/
cmuxUITests
仍能编译。被移动或重命名的符号可能会让
cmux
应用正常构建,但破坏测试目标(真实案例:
write(to:atomically:)
的拼写错误和移除的
TabManager.CommandResult
仅在
tests
作业中暴露)。推送包/重构更改前,请构建
cmux-unit
scheme(使用
-derivedDataPath /tmp/cmux-<tag>
,对于
cmuxApp
/
AppDelegate
的改动,需添加GlobalISel兼容标志),或让
tests
CI作业进行把关——绝不要仅将
reload.sh
作为测试可构建的依据。