行為 UI 測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
行為 UI 測試是分析 UI 階層,針對 UI 元素屬性做出斷言的測試。
Jetpack 架構
Jetpack 包含各種架構,提供用於編寫 UI 測試的 API:
- Espresso 測試架構 (Android 4.0.1、API 級別 14 以上) 提供 API,可用於編寫 UI 測試,模擬使用者與單一目標應用程式內 View 的互動情形。使用 Espresso 的主要優點是,它可自動將測試動作與您要測試的應用程式 UI 同步。Espresso 會偵測主執行緒的閒置時間,因此可在適當時間執行測試指令,進而提升測試的可靠性。
- Jetpack Compose (Android 5.0,API 級別 21 以上) 提供一組測試 API,可用於啟動及與 Compose 畫面和元件互動。與 Compose 元素的互動會與測試同步,並可完全控管時間、動畫和重組。
- UI Automator (Android 4.3,API 級別 18 以上) 是 UI 測試架構,適用於系統和已安裝應用程式之間的跨應用程式功能 UI 測試。您可以使用 UI Automator API 執行操作,例如在測試裝置上開啟「設定」選單或應用程式啟動器。
- Robolectric (Android 4.1,API 級別 16 以上) 可讓您在一般 JVM 中建立本機測試,在工作站或持續整合環境中執行,而非在模擬器或裝置上執行。可使用 Espresso 或 Compose 測試 API 與 UI 元件互動。
其他資源
如要進一步瞭解如何建立 UI 測試,請參閱下列資源。
說明文件
程式碼研究室
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Behavior UI Tests\n\nBehavior UI tests are tests that analyze the UI hierarchy to make assertions\non the properties of the UI elements.\n\nJetpack frameworks\n------------------\n\nJetpack includes various frameworks that provide APIs for writing UI tests:\n\n- The **[Espresso testing framework](/training/testing/espresso)** (Android 4.0.1, API level 14 or higher) provides APIs for writing UI tests to simulate user interactions with *Views* within a single target app. A key benefit of using Espresso is that it provides automatic synchronization of test actions with the UI of the app you are testing. Espresso detects when the main thread is idle, so it is able to run your test commands at the appropriate time, improving the reliability of your tests.\n- **[Jetpack Compose](/jetpack/compose)** (Android 5.0, API level 21 or higher) provides a set of [*testing APIs*](/jetpack/compose/testing) to launch and interact with Compose screens and components. Interactions with Compose elements are synchronized with tests and have complete control over time, animations and recompositions.\n- **[UI Automator](/training/testing/ui-automator)** (Android 4.3, API level 18 or higher) is a UI testing framework suitable for cross-app functional UI testing across system and installed apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher on a test device.\n- **[Robolectric](/training/testing/local-tests/robolectric)** (Android 4.1, API level 16 or higher) lets you create *local* tests that run on your workstation or continuous integration environment in a regular JVM, instead of on an emulator or device. It can use Espresso or Compose testing APIs to interact with UI components.\n\nAdditional resources\n--------------------\n\nFor more information about creating UI tests, consult the following resources.\n\n### Documentation\n\n- [Build instrumented tests](/training/testing/instrumented-tests)\n- [Espresso](/training/testing/espresso)\n- [Compose Testing](/jetpack/compose/testing)\n\n### Codelabs\n\n- [Introduction to Test Doubles and Dependency Injection](/codelabs/advanced-android-kotlin-training-testing-test-doubles)"]]