WWDC24 Meet Swift Testing

更新於 發佈於 閱讀時間約 11 分鐘


New Zealand — Deer Park Heights Queenstown

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 or throws
  • May be global actor-isolated (such as @MainActor )
raw-image

以前使用 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
raw-image


Required expectations

有時出現預期失敗,可能會希望提前結束測試,這時可以用 #require

  • Use try #require(...) to stop the test if the condition is false
raw-image
  • Can unwrap optional values, and stop test when nil
raw-image

Traits

  • Add descriptive information about a test
  • Customization whether a test runs
  • Modify how a test behaves
raw-image


Suites

  • Group related test functions and suites
  • Annotated using @Suite

Implicit for types containing @Test functions or suites

  • Use init and deinit for set-up and tear-down logic, respectively
  • Initialized once per instance @Test method

以前我們使用 XCTest 都用 class,例如

final class SomeTests: XCTestCase { ... }

現在建議使用 struct

raw-image


Common workflows


raw-image

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
raw-image


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


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


  • Use @available(...) to specify an OS availability condition
raw-image
  • Use @available(...) instead of checking at runtime using #available(...)
raw-image


Tests with common characteristics

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

raw-image

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

raw-image


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「參數化測試」

raw-image

優化後

raw-image


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

raw-image

優化後

raw-image


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


raw-image
raw-image
raw-image

鼓勵使用 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

而且在這些平台上都有通用的程式碼庫

raw-image


Wrap-up


  • Improve quality with Swift Testing
  • Customize tests with traits
  • Contribute in open source

最後可以了解更多細節 Go further with Swift Testing WWDC24



參考


留言
avatar-img
留言分享你的想法!
avatar-img
CHENGYANG的沙龍
0會員
15內容數
CHENGYANG的沙龍的其他內容
2025/06/24
一開始拿了一個 WWDC21 年的 demo 來敘述資料競爭的潛在問題,因為這 app 有三個 concurrent queue 在執行(Main queue, Background queue and Arbitrary queue),他們會交錯使用,很容就產生 data races。 我們
Thumbnail
2025/06/24
一開始拿了一個 WWDC21 年的 demo 來敘述資料競爭的潛在問題,因為這 app 有三個 concurrent queue 在執行(Main queue, Background queue and Arbitrary queue),他們會交錯使用,很容就產生 data races。 我們
Thumbnail
2025/06/17
Swift over the years 因為從 Swift 2 到 Swift 3,因為 Xcode 的關係,導致用戶一定要被迫升級 Swift 3,而且那次是大改版很難升級,但到 Swift 4 之後,Apple 團隊之後的解法是讓 Compiler 可以支持多種版本,不用
Thumbnail
2025/06/17
Swift over the years 因為從 Swift 2 到 Swift 3,因為 Xcode 的關係,導致用戶一定要被迫升級 Swift 3,而且那次是大改版很難升級,但到 Swift 4 之後,Apple 團隊之後的解法是讓 Compiler 可以支持多種版本,不用
Thumbnail
2025/06/08
這篇是閱讀了喵神的文章 TCA — SwiftUI 的救星?,這一系列喵神分成了四篇來講解,每篇都有許多重點,而且也是從易到難,很好閱讀,以下是個人的學習筆記,內容幾乎原文搬過來的,但也有一些是學習過程中額外補充的,因為有語法的更新,所以建議可以先閱讀原文,假如遇到語法不支援之類的問題,在參考這
Thumbnail
2025/06/08
這篇是閱讀了喵神的文章 TCA — SwiftUI 的救星?,這一系列喵神分成了四篇來講解,每篇都有許多重點,而且也是從易到難,很好閱讀,以下是個人的學習筆記,內容幾乎原文搬過來的,但也有一些是學習過程中額外補充的,因為有語法的更新,所以建議可以先閱讀原文,假如遇到語法不支援之類的問題,在參考這
Thumbnail
看更多
你可能也想看
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
本文介紹瞭如何在SwiftUI中調整元件的對齊方式,包括置中、向左/向右/向上/向下對齊的方法。透過調整HStack、VStack以及frame的maxWidth、maxHeight和alignment屬性,可以達到想要的對齊效果。
Thumbnail
本文介紹瞭如何在SwiftUI中調整元件的對齊方式,包括置中、向左/向右/向上/向下對齊的方法。透過調整HStack、VStack以及frame的maxWidth、maxHeight和alignment屬性,可以達到想要的對齊效果。
Thumbnail
本文檔介紹了在Swift中使用套件的詳細方法,包括如何引用第三方套件和自定義模組,如何創建自定義套件,以及一些常見的Swift套件。這些套件可以幫助開發者快速添加功能到項目中,提高開發效率和程式碼品質。
Thumbnail
本文檔介紹了在Swift中使用套件的詳細方法,包括如何引用第三方套件和自定義模組,如何創建自定義套件,以及一些常見的Swift套件。這些套件可以幫助開發者快速添加功能到項目中,提高開發效率和程式碼品質。
Thumbnail
這個章節主要介紹了Swift程式語言中物件導向程式設計的基本概念,包括類別、建構子、公開、私有、受保護等等的概念。同時,也介紹了繼承、多型、封裝、介面、抽象類別、靜態類別、列舉、委派、Lambda表達式、泛型和反射等進階特性。
Thumbnail
這個章節主要介紹了Swift程式語言中物件導向程式設計的基本概念,包括類別、建構子、公開、私有、受保護等等的概念。同時,也介紹了繼承、多型、封裝、介面、抽象類別、靜態類別、列舉、委派、Lambda表達式、泛型和反射等進階特性。
Thumbnail
本章節介紹了如何建立並設置Swift項目以及如何選擇和設置Swift代碼編輯器。這包括在Xcode和命令行中建立Swift項目,選擇Xcode、Visual Studio Code或AppCode作為編輯器,以及如何使用SPM安裝插件。
Thumbnail
本章節介紹了如何建立並設置Swift項目以及如何選擇和設置Swift代碼編輯器。這包括在Xcode和命令行中建立Swift項目,選擇Xcode、Visual Studio Code或AppCode作為編輯器,以及如何使用SPM安裝插件。
Thumbnail
這份文件的目的是介紹Swift語言,包括它的特性、應用範疇,以及誰在使用它。它也提供了一些學習Swift的資源和工具,以及一些常見的Swift庫和框架。
Thumbnail
這份文件的目的是介紹Swift語言,包括它的特性、應用範疇,以及誰在使用它。它也提供了一些學習Swift的資源和工具,以及一些常見的Swift庫和框架。
Thumbnail
需求情境: 在設計畫面時,資料來源是後台的 api,每一次畫面細節的修修改改,都會觸發 Xcode Preview 程序,導致不斷呼叫後台。此時若資料結構和大小都具有一定規模,就會導致效率低落,不斷等待,且消耗伺服器資源甚鉅。 解決方案: 將後台傳回的資料以檔案形式暫存在本地端,每次 pr
Thumbnail
需求情境: 在設計畫面時,資料來源是後台的 api,每一次畫面細節的修修改改,都會觸發 Xcode Preview 程序,導致不斷呼叫後台。此時若資料結構和大小都具有一定規模,就會導致效率低落,不斷等待,且消耗伺服器資源甚鉅。 解決方案: 將後台傳回的資料以檔案形式暫存在本地端,每次 pr
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News