> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/appendix/testing-system.md).

# Testing System

附錄 ⟩ Testing System

{% hint style="success" %}

* [XCTest](/ios/appendix/testing-system/xctest.md)：Swift 和 Objective-C 專案中常用的單元測試框架。
* [Swift Testing](/ios/appendix/testing-system/swift-testing.md)：現代化測試框架，Swift Package Manager 推薦選項。
  {% endhint %}

{% hint style="info" %}
:bulb: Xcode ⟩ Product ⟩ Test (<mark style="color:yellow;">**command + U**</mark>) 可以<mark style="color:yellow;">執行所有測試</mark> (Unit Tests)
{% endhint %}

{% tabs %}
{% tab title="🗺️ 圖表" %}

<figure><img src="https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2FVdzT1bCnVtraZMXKPtCR%2Ftesting_system_dialog.png?alt=media&amp;token=7e1b051b-57b9-4384-87a9-47fb8bec2dc2" alt=""><figcaption><p>Xcode ⟩ File ⟩ New ⟩ Package ... ⟩ framework ⟩ Libray ⟩ Testing System</p></figcaption></figure>
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="info" %}
If you choose to add tests to your project, Xcode will create **two** additional **targets**: one for <mark style="color:red;">**unit tests**</mark> and one for <mark style="color:red;">**UI tests**</mark>.
{% endhint %}

{% hint style="success" %}
structure for a test case:

* <mark style="color:red;">**Arrange**</mark>: arrange the "<mark style="color:orange;">**system under test**</mark>" (**SUT**) and the "<mark style="color:orange;">**inputs**</mark>".
* <mark style="color:red;">**Act**</mark>: act on the <mark style="color:orange;">**SUT**</mark> to get the "<mark style="color:orange;">**output**</mark>".
* <mark style="color:red;">**Assert**</mark>: assert the "**output**" <mark style="color:orange;">**matches**</mark> the "**expected result**".

```swift
func testEmptyMenuReturnsEmptySections() {
    
    // ⭐️ Arrange: (arrange inputs)
    let menu = [MenuItem]() 
    
    // ⭐️ Act: (generate output)
    let sections = menu.groupedByCategory()
    
    // ⭐️ Assert: (assert match)
    XCTAssertTrue(sections.isEmpty)
}
```

{% endhint %}
{% endtab %}

{% tab title="📙 書籍" %}

* Test-Driven Development in Swift (2021)
* [Test-Driven iOS Development with Swift - Fourth Edition](https://www.packtpub.com/product/test-driven-ios-development-with-swift-fourth-edition/9781803232485?utm_campaign=iOS%2BDev%2BWeekly\&utm_medium=email\&utm_source=iOS%2BDev%2BWeekly%2BIssue%2B555)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] KHAWER ⟩ [Unit Testing and UI Testing in Swift](https://khawerkhaliq.com/blog/swift-unit-testing-ui-testing/)
  {% endtab %}
  {% endtabs %}
