
New Zealand — Deer Park Heights Queenstown
- Building blocks 先了解核心概念
- Common workflows 討論幾種常見的工作流程,包括自定義測試或使用不同參數重複測試的方法。
- Swift Testing and XCTest 介紹 Swift Testing 和 XCTest 之間的相互關係
- Open source 這個新項目在開源社區中的角色
Building blocks
Test functions
- Annotated using
@Test
- Can be global functions or methods in a type
- May be
async
orthrows
- May be global actor-isolated (such as
@MainActor
)

以前使用 XCTest
要執行測試的 function 都要用 test 開頭命名,例如
func test_doSomething() throws { ... }
現在加上 @Test
就不用了
Expectations
Validate expected conditions
- Use
#expect(...)
to validate that an expected condition is true - Accepts ordinary expressions and operators
- Captures source code and subexpression values upon failure

Required expectations
有時出現預期失敗,可能會希望提前結束測試,這時可以用 #require
- Use
try #require(...)
to stop the test if the condition is false

- Can unwrap optional values, and stop test when
nil

Traits
- Add descriptive information about a test
- Customization whether a test runs
- Modify how a test behaves

Suites
- Group related test functions and suites
- Annotated using
@Suite
Implicit for types containing @Test
functions or suites
- Use
init
anddeinit
for set-up and tear-down logic, respectively - Initialized once per instance
@Test
method
以前我們使用 XCTest
都用 class,例如
final class SomeTests: XCTestCase { ... }
現在建議使用 struct

Common workflows

Tests with conditions
Runtime conditions
Controlling when tests run
- Specify a runtime-evaluated condition for a test using
.enabled(if:...)
- Test will be skipped if condition is false

- Unconditionally disable a test using
.disabled(...)
- Comment describing reason is included in results
- 這個方法比註解掉測試碼更好,因為他會驗證測試內的程式碼是否可編譯。

- Pair any trait with
.bug(...)
to reference an issue in a bug-tracking system

- Use
@available(...)
to specify an OS availability condition

- Use
@available(...)
instead of checking at runtime using#available(...)

Tests with common characteristics
利用 .tag(...)
,在左邊導航列可以找到相關被 tag 的測試,各 function 也可以在 @Test
上加 .tag

進階一點可以額外寫一個 struct 把 tag 寫在這層,原本各自 function 內的 tag 就可以移除了

Test Tags
Associate tests which have things in common to:
- Run all tests with a specific tag
- Filter by tag or see insights in Test Report
Shared among tests anywhere in a project
Tags can be local to a project or shared
擴充 Tag
extension Tag {
@Tag static var formatting: Self
@Tag static var networking: Self
}
Recommended practices
Prefer tags over test names when including/excluding in a Test Plan
Use the most appropriate trait type
- Example:
.enabled(if: ...)
instead of.tags(...)
to represent a condition
更進階的用法可以看 Go further with Swift Testing WWDC24
Tests with different arguments
假如我們有許多內容很像的測試,只是某部分參數不同,這時候就很適合用 Parameterized Tests「參數化測試」

優化後

再來一個例子,以下這個做法用 for-loop
循環重複多次單個測試,但這個做法不是最好的,最好就是用 parameterized test function

優化後

Parameterized testing
- View details of each argument in results
- Re-run individual arguments to debug
- Run arguments in parallel
更進階的用法可以看
Go further with Swift Testing WWDC24
https://developer.apple.com/wwdc24/10195
Swift Testing and XCTest



鼓勵使用 struct,因為是 value type,這樣做可以避免無意的狀態共享而導致的錯誤
Migrating from XCTest
✅ Recommended practices
- Share a single unit test target
- Swift Testing tests can coexist with XCTests
- Consolidate similar XCTests into a parameterized test
- Migrate each XCTest class with only one test method to a global
@Test
function - Remove redundant “test” prefix from method names
⚠️ Supported functionality
Continue using XCTest for tests which:
- Use UI automation APIs (such as
XCUIApplication
) - Use performance testing APIs (such as
XCTMetric
) - Can only be written in Objective-C
Avoid calling XCTAsssert(...)
from Swift Testing tests, or #expect(...)
from XCTests
更多細節可參考 Migrating a test from XCTest
Open source
它可以用於
- Apple Platforms
- Linux
- Windows
而且在這些平台上都有通用的程式碼庫

Wrap-up
- Improve quality with Swift Testing
- Customize tests with traits
- Contribute in open source
最後可以了解更多細節 Go further with Swift Testing WWDC24
參考
- https://wwdcnotes.com/documentation/wwdcnotes/wwdc24-10179-meet-swift-testing/
- https://leocoout.medium.com/welcome-swift-testing-goodbye-xctest-7501b7a5b304
- https://developer.apple.com/documentation/testing/migratingfromxctest
- https://developer.apple.com/videos/play/wwdc2024/10179/
- https://www.youtube.com/watch?v=WFnkNcvLnCI