使用 atest 測試平台程式碼
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android Studio for Platform (ASfP) 與 atest
指令列工具整合,可讓您直接從 IDE 在連線的裝置或模擬器上執行測試。
必要條件
- 使用 AOSP 原始碼開啟 ASfP 專案。
- 成功建構程式碼,並將其刷入裝置或模擬器。
- 使用
source build/envsetup.sh
和 lunch
初始化建構環境。
執行測試
您可以使用 ASfP 中的 atest
,透過下列幾種方式執行測試:
空白邊執行圖示:按一下「執行」圖示 play_circle
,即可執行該特定測試。
右鍵選單:在「Project」視窗或編輯器中,對測試檔案、類別或方法按一下滑鼠右鍵,然後選取「Run 'atest'」。
使用終端機:在 ASfP 中開啟整合式終端機 (依序選取「View」>「Tool Windows」>「Terminal」),然後直接執行 atest
指令。例如:bash
atest MyTestModule
atest MyTestClass#myTestMethod
查看測試結果
測試結果會顯示在 ASfP 的「Run」工具視窗中。這個視窗會顯示測試進度、通過或失敗狀態,並提供每項測試的記錄和輸出內容。
測試設定
從空白邊或按一下滑鼠右鍵選單執行測試時,ASfP 會自動建立暫時的執行或偵錯設定。您可以修改並儲存這些設定,以便使用特定選項或標記快速重新執行測試:
- 依序前往「Run」>「Edit Configurations」。
- 找出要修改的
atest
設定,或按一下「+」按鈕並選取「atest」,建立新的設定。
- 指定測試模組、類別和方法,並新增任何必要的
atest
指令列選項。
測試訣竅
- 目標測試:只執行所需測試,節省時間。使用
Module:Class#Method
格式或其他 atest
篩選器。
- 使用模擬器快照:如要在模擬器上加快測試執行速度,建議使用快照快速還原至已知良好狀態。
- 檢查記錄:使用「Run」工具視窗中的記錄,診斷任何測試失敗情形。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-09-05 (世界標準時間)。
[[["容易理解","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-09-05 (世界標準時間)。"],[],[],null,["Android Studio for Platform (ASfP) integrates with the `atest` command-line\ntool, letting you run tests on your connected device or emulator directly from\nthe IDE.\n\nPrerequisites\n\n- Open an ASfP project with your AOSP source code.\n- Successfully build and flash your code to a device or emulator.\n- Initialize your build environment using `source build/envsetup.sh` and `lunch`.\n\nRun tests\n\nYou can run tests using `atest` in ASfP in several ways:\n\n- **Gutter run icons:** Click the **Run** icon play_circle\n\n next to a test class or method in the editor to run that specific test.\n- **Right-click menu:** Right-click on a test file, class, or method in the\n Project window or editor and select **Run 'atest'**.\n\n- **Using the terminal:** Open the integrated terminal in ASfP (**View \\\u003e Tool\n Windows \\\u003e Terminal** ) and run `atest` commands directly. For example: `bash\n atest MyTestModule\n atest MyTestClass#myTestMethod`\n\nView test results\n\nTest results are displayed in the **Run** tool window within ASfP. This window\nshows the test progress, pass or fail status, and provides logs and output for\neach test.\n\nTest configurations\n\nWhen you run a test from the gutter or right-click menu, ASfP automatically\ncreates a temporary run or debug configuration. You can modify and save these\nconfigurations to quickly re-run tests with specific options or flags:\n\n1. Go to **Run \\\u003e Edit Configurations**.\n2. Find the `atest` configuration you want to modify or create a new one by clicking the **+** button and selecting **atest**.\n3. Specify the test module, class, method, and add any necessary `atest` command-line options.\n\nTips for testing\n\n- **Targeted testing:** Run only the specific tests you need to save time. Use the format `Module:Class#Method` or other `atest` filters.\n- **Use emulator snapshots:** For faster test runs on emulators, consider using snapshots to quickly revert to a known good state.\n- **Check logs:** Use the logs in the **Run** tool window to diagnose any test failures."]]