# 讓 Agent 執行 App 的測試

> 請 Agent 執行 App 的測試，在聊天中追蹤執行過程，閱讀結果並請它修正。

Source: https://air.build/tw/docs/build/run-your-apps-tests/

你的 Agent 可以執行專案中的自動化測試，並告訴你哪些通過、哪些失敗。你在專案的聊天中提出要求。Agent 會用 `runtest` 指令執行測試（請參閱 [runtest](https://air.build/tw/docs/cli/runtest/)），並在聊天中回報結果。

測試在哪裡執行取決於你的團隊：

| 團隊 | 測試在哪裡執行 |
| --- | --- |
| 雲端團隊 | 在雲端的 Mac 建置機上。Android 版本（Beta 版）的測試在 Linux 建置機上執行。Agent 在 Linux 建置機上執行的其他腳本使用 [runlinux](https://air.build/tw/docs/cli/runlinux/)。 |
| 本機團隊 | 在那台 Mac 上。 |

測試執行不會使用你的預覽裝置，所以你不會在預覽中看到它。如果你想看 Agent 在裝置上操作你的 App，請讓它在預覽中試用 App。請參閱[在預覽中執行你的 App](https://air.build/tw/docs/build/preview-your-app/)。

## 要求執行測試

在專案的聊天中要求執行測試，例如：

```text
Run the app's tests and tell me what fails.
```

如果只想執行其中一部分，請說明是哪些，例如「只執行登入相關的測試。」

專案的測試列在其 `airbuild.yaml` 檔案的 `test:` 下，每個測試都有一個名稱。如果還沒有測試，請讓 Agent 先寫一些，然後執行：

```text
Add tests for sign-in and checkout, then run them.
```

## 追蹤執行過程

測試執行期間，訊息框上方的一行會顯示 Agent 所說的正在做的事。請參閱[在聊天中與 Agent 合作](https://air.build/tw/docs/projects/work-with-agents-in-chat/#see-what-agents-are-doing)。如果這次執行屬於某個任務，任務卡片也會顯示相同的內容。請參閱[用任務追蹤 Agent 的工作](https://air.build/tw/docs/projects/track-work-with-tasks/)。

開啟試驗性功能 **顯示 Agent 正在做什麼** 後，團隊擁有者還可以即時查看 Agent 的工具和輸出。請參閱[查看 Agent 正在做什麼](https://air.build/tw/docs/agents/watch-agents-work/)。

執行會一直持續到完成，即使 Agent 被打斷也一樣，Agent 之後可以接著處理。若要停止執行，請這樣說：

```text
Cancel the test run.
```

## 閱讀結果

Agent 會在聊天中告訴你測試是否通過。測試指令本身不會發布卡片，但 Agent 可能會傳送一張列出失敗項目的報告卡片。如果你想要一張，請直接提出。請參閱[閱讀 Agent 傳來的圖表或檢查報告](https://air.build/tw/docs/agents/read-charts-and-checks-from-agents/#read-a-report)。

## 要求修正

在聊天中回覆，例如：

```text
Fix the two failing tests and run them again.
```

測試失敗可能是 App 有問題，也可能是測試有問題。如果你不確定是哪一種，請在 Agent 改動任何一方之前先問它：

```text
Is the app wrong, or is the test out of date?
```

改動之後，Agent 會開始新一輪執行。當它重新建置 App 後，你可以在預覽中檢查修正結果。請參閱[新建置就緒時打開預覽](https://air.build/tw/docs/build/open-the-preview-when-a-build-is-ready/)。

## 其他測試方式

| 項目 | 在哪裡查看 |
| --- | --- |
| App 的 Android 版本（Beta 版）的測試 | 用同樣的方式要求，例如「執行 Android 測試。」請參閱[製作 Android 或 PocketJS 版本（Beta 版）](https://air.build/tw/docs/build/android-and-pocketjs/)。 |
| 含有 **測試** 動作的 Xcode Cloud 工作流程 | 專案的 **Xcode Cloud** 標籤頁會顯示每次執行的測試失敗。請參閱[使用 Xcode Cloud 建置](https://air.build/tw/docs/app-store/build-with-xcode-cloud/)。 |
| 讓他人試用 Beta 版 | 請參閱[用 TestFlight 分享 Beta 版](https://air.build/tw/docs/app-store/test-with-testflight/)。 |

## 常見問題

**可以同時進行多次測試執行嗎？**

在雲端團隊中可以。本機團隊一次只會執行一個。
