自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
自動化測試基礎架構
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 9 包含供應商測試套件 (VTS) 基礎架構,可在搭載 AOSP 通用系統映像檔 (GSI) 的合作夥伴裝置上,自動執行 VTS、CTS 或其他測試。先前執行這些測試時,必須仰賴大量手動操作;新的 VTS 測試基礎架構則可支援每天在多部裝置上執行多次自動化測試。
建築
VTS 自動化測試基礎架構採用以下架構:
圖 1. VTS 自動化測試基礎架構架構
當測試觸發時,VTS 自動化測試基礎架構會執行下列任務:
- 從不同位置擷取建構構件和測試資源:
- 合作夥伴 Android 版本 (PAB)。適用於 GSI、VTS 架構和其他一些版本。
- 本機檔案系統、Google Cloud Storage 或其他供應商專屬的建構系統。適用於未將版本儲存在 Google 雲端的合作夥伴。
- 將建構成果 (來自裝置) 和 GSI (來自 Android 開放原始碼計畫) 刷入已連結的裝置。
- 使用本機 TradeFed 或雲端中的 TradeFed 執行 VTS 測試。
- 將測試結果回報至 VTS 資訊主頁
這個程序由 VTS 主機控制器 (HC) 協調,這是研究室中的機器,可指揮所有已連線的測試裝置行為。HC 負責擷取最新版本、將其刷新至裝置,以及叫用測試 (在本機或透過指揮官)。它也會與雲端排程器進行通訊,並將流量導向排程器和在 HC 上執行的 TradeFed 例項 (或其他某些測試套件) 之間。如要進一步瞭解主控器,請參閱「主控器架構」。
資源供應者
自動化測試需要系統建構、測試檔案和 VTS 構件等資源。雖然可以從原始碼建構這些檔案,但從樹狀結構頂端定期建構這些檔案,然後發布供下載的構件,會更容易。
合作夥伴可以透過下列位置存取自動化資源:
- 合作夥伴 Android 版本。按帳戶分別授予程式輔助存取權。
- 本機檔案系統 (或類似項目)。適用於未使用合作夥伴 Android 版本的合作夥伴。
為了日後在裝置上執行閃燈作業,資源會納入兩種選項的建構提供者,這些提供者是從單一 build_provider.py
延伸而來,該 build_provider.py
會在本機暫存目錄中儲存建構。
合作夥伴 Android 版本
在 Android 8.1 以下版本中,Android 合作夥伴必須前往合作夥伴 Android 建構網站 (https://partner.android.com/build),前往自己的帳戶,然後透過使用者介面擷取最新的系統映像檔。為協助合作夥伴避免這個耗時且費時的程序,Android 9 支援在提供適當的憑證時,自動從 PAB 下載這些資源。
建立存取權
程式存取權會使用 Google API 上的 OAuth2 存取必要的 RPC。如要使用標準方法產生 OAuth2 憑證,合作夥伴必須與 Google 設定用戶端 ID/密鑰組合。當 PartnerAndroidBuildClient
首次指向該機密金鑰時,會開啟瀏覽器視窗,讓使用者登入 Google 帳戶,進而產生繼續操作所需的 OAuth2 憑證。憑證 (存取權杖和重新整理權杖) 會儲存在本機,也就是說合作夥伴只需登入一次。
網址的 POST 要求
按一下 PAB 中的資源連結,系統會傳送 POST 要求,其中包含該資源的必要資料,包括:
- 版本 ID、建構目標
- 資源名稱
- 分支版本
- 候選版名稱,以及候選版是否為內部版本
buildsvc
RPC 的 downloadBuildArtifact
方法會收到 POST 要求,並傳回可用於存取資源的網址。
- 針對 Clockwork Companion APK 資源,網址是託管在 PAB 上的可讀網址 (該網址受到驗證保護,且可透過適當的 OAuth2 憑證存取)。
- 對於其他資源,網址是來自內部 Android Build API 的長型未受保護網址 (五分鐘後會過期)。
取得網址
為避免跨網站偽造要求,buildsvc
RPC 需要將 XSRF 權杖與其他參數一併 POST。雖然這類權杖可讓程序更安全,但也讓程式輔助存取變得更加困難,因為現在存取權需要權杖 (僅適用於 PAB 頁面的 JavaScript)。
為避免這個問題,Android 9 重新設計了所有檔案 (不只限於 APK) 的網址命名配置,以便使用可預測的網址名稱存取構件清單和構件網址。現在,PAB 採用方便的網址格式,讓合作夥伴下載資源;HC 指令碼可輕鬆下載這些 APK,因為它們知道網址格式,且 HC 不需 buildsvc
RPC,因此可略過 XSRF/cookie 問題。
本機檔案系統
在提供包含成果列表 (或 ZIP 檔案) 的目錄後,建構提供者會根據目錄中的內容設定相關圖片。您可以使用 gsutil 工具,將檔案從 Google Cloud Storage 複製到本機目錄。
Flash 版本
最新的裝置映像檔下載至主機後,必須將這些映像檔刷新到裝置上。這項作業會使用標準 adb
和 fastboot
指令和 Python 子程序,並根據建構提供者儲存的暫存檔案路徑執行。
支援的動作:
- 僅刷新 GSI
- 從主要系統閃爍個別圖片 (例如
fastboot flash boot boot.img
)
- 從主系統刷新所有映像檔。示例:
fastboot flashall
(使用內建的 flashall
公用程式)
fastboot flash
(一次一個)
執行測試
在 Android 9 中,VTS 自動化測試基礎架構僅支援 TradeFed 測試輔助程式,但日後可擴充支援其他輔助程式。
準備好裝置後,您可以使用下列其中一個選項叫用測試:
- 在本機使用 TradeFed 時,請在主機控制器中使用
test
指令,該指令會採用 VTS 測試計畫的名稱 (例如 vts-selftest
) 並執行測試。
- 使用 TradeFed 叢集 (可選擇連線至 MTT) 時,請在主控台控制台中使用
lease
指令,查看未執行完畢的測試。
如果使用 TradeFedCluster,TradeFed 會在本機上做為遠端管理員執行。如果沒有,則會使用 Python 子程序叫用測試。
報表結果
VtsMultiDeviceTest
會自動將測試結果回報給部分 VTS 資訊主頁專案。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Automated testing infrastructure\n\nAndroid 9 includes a Vendor Test Suite (VTS)\ninfrastructure for automated testing of VTS, CTS, or other tests on partner\ndevices running the AOSP generic system image (GSI). Previously, running these\ntests was a highly manual operation; the new VTS test infrastructure is\ndesigned to support automated testing multiple times a day on multiple\ndevices.\n\nArchitecture\n------------\n\n\nThe VTS automated testing infrastructure uses the following architecture:\n\n\n**Figure 1.** VTS automated testing infrastructure architecture\n\n\nWhen a test is triggered, the VTS automated testing infrastructure performs\nthe following tasks:\n\n1. Fetches build artifacts and test resources from different locations:\n - **Partner Android Build (PAB)**. For the GSI, VTS framework, and some other builds.\n - **Local filesystem, Google Cloud Storage, or other vendor-specific\n build system**. For partners who do not store builds in Google's cloud.\n2. Flashes build artifacts (from the device) and the GSI (from AOSP) onto the connected device(s).\n3. Runs VTS tests using local TradeFed or a TradeFed in the cloud.\n4. Reports test results to the VTS dashboard\n\n\nThe process is coordinated by the VTS host controller (HC), a machine in the\nlab that directs the behavior of all connected devices under test. The HC is\nresponsible for fetching the latest builds, flashing them onto devices, and\ninvoking tests (either locally or through the commander). It also communicates\nwith a cloud scheduler and directs traffic between the scheduler and the\nTradeFed instance (or some other harness) running on the HC. For details on\nthe host controller, see [Host\nController Architecture](/docs/core/tests/vts/host-controller).\n\nResource providers\n------------------\n\n\nAutomated testing requires resources such as system builds, test files, and\nVTS artifacts. While it's possible to build these from source, it is easier to\nbuild them from tip-of-tree regularly then post the artifacts for download.\n\n\nPartners can access automation resources using the following locations:\n\n- **Partner Android Build**. Programmatic access granted on a per-account basis.\n- **Local filesystem** (or similar). For partners who do not use the Partner Android Build.\n\n\nFor use in flashing the devices later, resources include build providers for\nboth options, extending from a single `build_provider.py` that\nstores the builds in local temporary directories.\n\n### Partner Android Build\n\n\nIn Android 8.1 and lower releases, Android partners were required to visit the\nPartner Android Build website\n(\u003chttps://partner.android.com/build\u003e),\nnavigate to their account, and fetch the latest system images through the user\ninterface. To help partners avoid this slow and labor-intensive process,\nAndroid 9 includes support for automatically\ndownloading these resources from PAB when\nthe appropriate credentials are provided.\n\n#### Establish access\n\n\nProgrammatic access uses OAuth2 on Google APIs to access the required RPCs.\nUsing the\n[standard\napproach](https://developers.google.com/api-client-library/python/guide/aaa_oauth#flow_from_clientsecrets) for generating OAuth2 credentials, the partner must set up a\nclient id/secret pair with Google. When the\n`PartnerAndroidBuildClient` is pointed to that secret for the first\ntime, it opens a browser window for the user to log in to their Google\naccount, which generates the OAuth2 credentials needed to move forward. The\ncredentials (access token and refresh token) are stored locally, meaning\npartners should need to login only once.\n\n#### POST request for URL\n\n\nClicking a resource link in PAB sends a POST request that includes the\nnecessary data for that resource, including:\n\n- build id, build target\n- resource name\n- branch\n- release candidate name and whether or not the candidate is an internal build\n\n\nThe POST request is received by the `downloadBuildArtifact` method\nof the `buildsvc` RPC, which returns a URL that can be used to\naccess the resource.\n\n- For Clockwork Companion APK resources, the URL is a readable URL hosted on PAB (which is auth-protected and accessible with the appropriate OAuth2 credentials).\n- For other resources, the URL is long, non-protected URL from the internal Android Build API (which expires after five minutes).\n\n#### Get the URL\n\n\nTo avoid cross-site request forgery, the `buildsvc` RPC requires an\nXSRF token to be POSTed with the other parameters. While this token makes the\nprocess more secure, it also makes programmatic access much harder since the\ntoken (which is available only in the JavaScript of the PAB page) is now also\nrequired for access.\n\n\nTo avoid this issue, Android 9 redesigns the URL\nnaming scheme for all files (not just APKs) to use predictable URL names for\naccessing artifact lists and artifact URLs. The PAB now uses a convenient URL\nformat that enables partners to download resources; HC scripts can download\nthose APKs easily, since the URL format is known, and HC can bypass the\nXSRF/cookie issues because it does not need the `buildsvc` RPC.\n\n### Local filesystem\n\n\nGiven a directory with a list (or zip file) of artifacts, the build provider\nsets the relevant images based on what's in the directory. You can use the\n[gsutil](https://cloud.google.com/storage/docs/gsutil)\ntool to copy files from Google Cloud Storage to a local directory.\n\nFlash builds\n------------\n\n\nAfter the most recent device images are downloaded to the host, those images\nmust be flashed onto the devices. This is done using the standard\n`adb` and `fastboot` commands and Python subprocesses,\nbased on the temporary file paths stored by the build providers.\n\n\nSupported actions:\n\n- Flashing only the GSI\n- Flashing individual images from the main system (e.g., `fastboot flash boot boot.img`)\n- Flashing all images from the main system. Example:\n - `fastboot flashall` (using the built-in `flashall` utility)\n - `fastboot flash` (one at a time)\n\nRun tests\n---------\n\n\nIn Android 9, the VTS automated testing\ninfrastructure supports only the TradeFed test harness but could be extended\nto support other harnesses in the future.\n\n\nAfter the devices are prepared, you can invoke tests using one of the\nfollowing options:\n\n- When using TradeFed locally, use the `test` command in the host controller, which takes the name of a VTS test plan (e.g. `vts-selftest`) and runs the test.\n- When using a TradeFed Cluster (optionally connected to MTT), use the `lease` command in the host controller console, which looks for unfulfilled test runs.\n\n\nIf using TradeFedCluster, TradeFed runs\n[locally as a remote manager](/docs/core/tests/vts/host-controller).\nIf not, the tests are invoked using Python subprocesses.\n\nReport results\n--------------\n\n\nTest results are automatically reported to some VTS dashboard projects by\n`VtsMultiDeviceTest`."]]