# Xcode 測試見解 {#xcode-test-insights}

Tuist Test Insights 提供 Xcode 測試分析，協助你透過識別緩慢的測試或快速了解失敗的 CI 執行，來監控測試套件的健全性。隨著測試套件規模擴大，要察覺如測試逐漸變慢或間歇性失敗等趨勢會變得越來越困難。Tuist Test Insights 提供你維持快速且可靠測試套件所需的可見度。

有了 Test Insights，你可以回答以下問題：
- 我的測試是否變慢了？是哪些測試？
- 哪些測試不穩定且需要關注？
- 為什麼我的 CI 執行失敗了？

## 設定 {#setup}

若要開始追蹤你的測試，你可以將 `tuist inspect test` 指令加入至 scheme 的測試後置動作中來使用：

![用於檢查測試的後置動作](/images/guides/features/build-insights/inspect-test-scheme-post-action.png)

若你正在使用 [Mise](https://mise.jdx.dev/)，你的腳本需要在後置動作環境中啟用 `tuist`：
```sh
# -C ensures that Mise loads the configuration from the Mise configuration
# file in the project's root directory.
$HOME/.local/bin/mise x -C $SRCROOT -- tuist inspect test
```

> [!TIP]
> **Mise 與專案路徑**
>
> 你環境中的 `PATH` 環境變數不會被 scheme 後置動作繼承，因此你必須使用 Mise 的絕對路徑，這取決於你安裝 Mise 的方式。此外，別忘了從專案中的某個 target 繼承建置設定，這樣你才能從 $SRCROOT 指向的目錄執行 Mise。

只要你已登入 Tuist 帳號，系統就會追蹤你的測試執行。你可以在 Tuist 儀表板中存取測試見解，並觀察它們隨時間的變化：

![包含測試見解的儀表板](/images/guides/features/build-insights/tests-dashboard.png)

除了整體趨勢外，你也可以深入檢視每個單獨的測試，例如在 CI 上除錯失敗或緩慢的測試時：

![測試詳細資訊](/images/guides/features/build-insights/test-detail.png)

## 產生的專案 {#generated-projects}

> [!NOTE]
> 自動產生的 scheme 會自動包含 `tuist inspect test` 後置動作。
>
> 若你無意在自動產生的 scheme 中追蹤測試見解，請使用 [testInsightsDisabled](https://projectdescription.tuist.dev/documentation/projectdescription/tuist) 產生選項將其停用。

若你使用帶有自訂 scheme 的產生專案，可以為測試見解設定後置動作：

```swift
let project = Project(
    name: "MyProject",
    targets: [
        // Your targets
    ],
    schemes: [
        .scheme(
            name: "MyApp",
            shared: true,
            buildAction: .buildAction(targets: ["MyApp"]),
            testAction: .testAction(
                targets: ["MyAppTests"],
                postActions: [
                    // Test insights: Track test duration and flakiness
                    .executionAction(
                        title: "Inspect Test",
                        scriptText: """
                        $HOME/.local/bin/mise x -C $SRCROOT -- tuist inspect test
                        """,
                        target: "MyAppTests"
                    )
                ]
            ),
            runAction: .runAction(configuration: "Debug")
        )
    ]
)
```

若你未使用 Mise，你的腳本可以簡化為：

```swift
testAction: .testAction(
    targets: ["MyAppTests"],
    postActions: [
        .executionAction(
            title: "Inspect Test",
            scriptText: "tuist inspect test"
        )
    ]
)
```

## 持續整合 {#continuous-integration}

若要在 CI 上追蹤測試見解，你需要確保你的 CI 已<.localized_link href="/guides/server/authentication#continuous-integration">通過驗證</.localized_link>。

此外，你需要執行以下其中一項操作：
- 在叫用 `xcodebuild` 動作時，使用 <.localized_link href="/cli/xcodebuild#tuist-xcodebuild">`tuist xcodebuild`</.localized_link> 指令。
- 將 `-resultBundlePath` 加入至你的 `xcodebuild` 叫用中。

當 `xcodebuild` 在未使用 `-resultBundlePath` 的情況下測試你的專案時，不會產生所需的結果套件檔案。`tuist inspect test` 後置動作需要這些檔案來分析你的測試。

### Git 歷史記錄 {#git-history}

在 CI 上，Tuist 也會上傳檢出的提交歷史記錄：提交 SHA、其父提交、提交日期，以及執行與其拉取請求基礎分支所在的分支。不會上傳任何檔案內容、提交訊息或作者資訊。這能讓 Tuist 知道哪些提交已合併至你的預設分支，例如合併佇列所測試的提交。Tuist 會讀取检出已具備的歷史記錄，若检出沒有基礎分支，則會向遠端查詢其最新提交（`git ls-remote`）。在非程式碼涵蓋率情境下，它不會擷取歷史記錄。在使用程式碼涵蓋率時，會深化淺層检出（不包含檔案內容），以找出拉取請求的分叉點並到達 Tuist 已具備的提交，確保絕不會在上傳提交時缺少其父提交。

| 變數 | 效果 |
| --- | --- |
| `TUIST_GIT_UPLOAD_ENABLED=false` | 不會讀取或上傳任何 Git 歷史記錄。 |
| `TUIST_GIT_UNSHALLOW_ENABLED=false` | 僅讀取检出，不會聯絡遠端。 |
