自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
納入單元和功能測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本節說明如何在對專案進行變更後,執行並提供 Trade Federation 測試。蒐集的資料包括:
- 新增單元測試類別的位置
- 在 Eclipse 和 Eclipse IDE 外執行單元測試
- 新增功能測試的位置
- 執行功能測試
- 在本機執行部分 TF 提交前驗證
新增單元測試
在 Android 開放原始碼計畫 (AOSP) 中,請在以下位置新增單元測試類別:tools/tradefederation/core/tests/src/com/android/tradefed/UnitTests.java
重要事項:將新的單元測試類別新增至這些位置後,系統會在提交前自動執行這些類別,無須額外設定。
執行單元測試
Trade Federation 的所有 AOSP 單元測試和功能測試都位於 tools/tradefederation/core/tests
專案中。
在 Eclipse 中,如要執行個別單元測試,只要在測試上按一下滑鼠右鍵,然後選取「Run As」>「JUnit」即可。如要執行所有單元測試,請執行 com.android.tradefed.UnitTests 套件。
您也可以在建構後,透過 Tradefed 來源樹狀結構中的指令列啟動單元測試,如下所示:
tools/tradefederation/core/javatests/run_tradefed_tests.sh
單元測試可以獨立執行,但功能測試應使用 Trade Federation 本身執行,因為這類測試需要 Android 裝置。所有功能測試都應遵循 *FuncTest
命名慣例。
查看單元測試結果
執行 run_tradefed_tests.sh
時,單元測試會測試測試輔助工具的所有部分,包括某些錯誤狀態。這會將詳細輸出內容 (包括堆疊追蹤) 列印到主控台。
結果的最終摘要會指出是否發生失敗。
主控台中的最終摘要範例:
12:55:02 ========== Result Summary ==========
Results summary for test-tag 'TFUnitTests': 3355 Tests [3355 Passed]
所有測試都會維持通過狀態,因此如果本機變更發生任何失敗,請務必修正測試。
新增功能測試
執行功能測試
如要透過 Eclipse 執行功能測試,請按照下列步驟操作:
- 請確認裝置已連上主機,且
adb
和 fastboot
(如有必要) 已納入 Eclipse 的 PATH。最簡單的方法,就是透過 shell 設定啟動 Eclipse,並使用適當的 PATH。
- 建立 Java 應用程式。透過「Run」>「Run Configurations」執行設定。
- 將專案設為
tradefed-tests
,並將主要類別設為 com.android.tradefed.command.CommandRunner
。
- 執行
m tradefed-all
。
- 在「Arguments」分頁中提供下列指令列引數:
host
--class <full path of test class to run>
- 按一下「Run」。
如何在 Eclipse 外執行功能測試。
- 建立 Trade Federation。
- 將 Android 裝置連接到主機。
- 執行
tools/tradefederation/core/javatests/run_tradefed_func_tests.sh
- 您可以選擇在
adb devices
的輸出內容中附加 --serial <serial no>
,藉此選擇裝置。
針對本機變更執行 TF 提交前測試
如果您想以與 TF 預提交類似的方式執行,請使用以下指令碼:
tools/tradefederation/core/javatests/run_tradefed_aosp_presubmit.sh
這會針對您在本機建構的 TF 觸發所有 TF 提交前測試,協助您驗證變更不會破壞任何測試。
TF 提交前測試是上述單元測試的超集,但執行速度較慢。因此,建議您在開發期間執行單元測試,以便更快速驗證,並在上傳 CL 之前執行預提交作業。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Include unit and functional tests\n\nThis section explains how to run and contribute to the Trade Federation tests\nafter making a change to the project. This includes:\n\n- Where to add the new unit test classes\n- Running unit tests in Eclipse and outside the Eclipse IDE\n- Where to add functional tests\n- Running the functional tests\n- Running some of TF presubmit validation locally\n\nAdd unit tests\n--------------\n\nIn the Android Open Source Project (AOSP), add the unit tests class in:\n[tools/tradefederation/core/tests/src/com/android/tradefed/UnitTests.java](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/tests/src/com/android/tradefed/UnitTests.java)\n\nIMPORTANT: Adding your new unit test classes to these locations will result in\nthem automatically running in presubmit without additional setup.\n\nRun unit tests\n--------------\n\nAll the AOSP unit tests and functional tests for Trade Federation are located in\nthe\n[`tools/tradefederation/core/tests`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/tests/)\nproject.\n\nInside Eclipse, to run an individual unit test, simply right-click the test and\nselect **Run As \\\u003e JUnit** . To run all unit tests, run the\n**com.android.tradefed.UnitTests** suite.\n\nYou can also start the unit test from the command line in the Tradefed source\ntree after building, like so:\n`tools/tradefederation/core/javatests/run_tradefed_tests.sh`\n\nUnit tests can be executed standalone, but functional tests should be executed\nby using Trade Federation itself; they require an Android device. All functional\ntests should follow the naming convention `*FuncTest`.\n\n### Check unit tests results\n\nWhen running `run_tradefed_tests.sh`, the unit tests are exercising all parts of\nthe test harness including some error conditions. It will print verbose output\nto the console, including stack traces.\n\nThe final summary of the results will indicate if a failure occurred or not.\n\nExample final summary in the console: \n\n 12:55:02 ========== Result Summary ==========\n Results summary for test-tag 'TFUnitTests': 3355 Tests [3355 Passed]\n\nAll tests are maintained to pass, so if any failure occurs on your local change\nplease ensure the tests are fixed.\n\nAdd functional tests\n--------------------\n\n- If your functional test involves a device (uses any `ITestDevice` API) the\n suite definition is located in [com.android.tradefed.DeviceFuncTests](https://cs.android.com/android/platform/superproject/+/android-latest-release:tools/tradefederation/core/javatests/com/android/tradefed/DeviceFuncTests.java).\n Otherwise, the suite definition is located in [com.android.tradefed.FuncTests](https://cs.android.com/android/platform/superproject/+/android-latest-release:tools/tradefederation/core/javatests/com/android/tradefed/FuncTests.java).\n\n- If your test methods make sense to put in one of the existing suite sub-classes,\n prefer to add it there. Otherwise, feel free add a new class to the applicable\n suite.\n\n- In either case, by adding it to the suite, your test is automatically run\n in the CI pipeline with the other functional tests.\n\nRun functional tests\n--------------------\n\nTo run a functional test from Eclipse:\n\n1. Ensure a device is connected to the host and that `adb` and if necessary `fastboot` are in Eclipse's PATH. The easiest way to do this is to launch Eclipse from a shell setup with the proper PATH.\n2. Create a Java application. Run configuration via **Run \\\u003e Run\n configurations**.\n3. Set project to `tradefed-tests` and the main class to `com.android.tradefed.command.CommandRunner`.\n4. Run `m tradefed-all`.\n5. Provide the following command line arguments in the *Arguments* tab: `host\n --class \u003cfull path of test class to run\u003e`\n6. Click **Run**.\n\nTo run functional tests outside Eclipse.\n\n1. Build Trade Federation.\n2. Connect an Android device to the host.\n3. Run `tools/tradefederation/core/javatests/run_tradefed_func_tests.sh`\n4. Optionally, choose the device by appending `--serial \u003cserial no\u003e` as it appears in the output of `adb devices`.\n\nRun TF presubmit tests against local changes\n--------------------------------------------\n\nIf you want to run in a similar way as the TF presubmit, use this: \n\n tools/tradefederation/core/javatests/run_tradefed_aosp_presubmit.sh\n\nThis will trigger all the TF presubmit tests against your locally built TF to\nhelp you validate that your change is not breaking any tests.\n\nTF presubmit tests are a superset of the unit tests above, but it is slower to\nrun them. So it is recommended to run the unit tests during development for\nquicker validation and to run the presubmits before uploading the CL."]]