使用 Android Studio 執行測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Firebase Test Lab 提供雲端式基礎架構,可測試 Android 應用程式,並與 Android Studio 完全整合,方便您執行插樁測試及查看測試結果。
本指南說明如何在 Android Studio 中修改檢測設備測試,以便整合及執行 Test Lab。如要瞭解如何使用 Android Studio UI 中的 Test Lab 建立測試矩陣、執行檢測設備測試及查看測試結果,請參閱「使用 Firebase Test Lab 執行測試」。
擷取螢幕截圖
Test Lab 可支援在執行插樁測試時擷取螢幕截圖。如要瞭解如何擷取螢幕截圖,請參閱將螢幕截圖程式庫新增至專案。
使用 Espresso Test Recorder 建立測試
您可以透過 Espresso Test Recorder 工具為應用程式建立 UI 測試,無須另外寫程式碼來測試。您可以記錄與裝置的互動,然後在應用程式的特定快照中,新增用來驗證 UI 元素的斷言。接下來,Espresso Test Recorder 會擷取儲存的記錄,並自動產生相對應的 Espresso UI 測試,供您在 Test Lab 中測試應用程式。
詳情請參閱「使用 Espresso Test Recorder 建立 UI 測試」。
修改「Test Lab」的檢測設備測試行為
Test Lab 提供系統變數,您可以將該變數新增至插碼測試,以便在 Test Lab 中執行測試時,讓測試的行為與在自己的測試裝置或模擬器上執行測試時不同。
下列程式碼範例會讀取系統屬性 firebase.test.lab
,並在測試於 Test Lab 中執行時,將字串 testLabSetting
設為 true
。然後,系統會使用這個字串的值,控管是否執行其他陳述式:
Kotlin
val testLabSetting = Settings.System.getString(contentResolver, "firebase.test.lab")
if ("true" == testLabSetting) {
// Do something when running in Test Lab
// ...
}
Java
String testLabSetting = Settings.System.getString(getContentResolver(), "firebase.test.lab");
if ("true".equals(testLabSetting)) {
// Do something when running in Test Lab
// ...
}
透過 Firebase Test Lab 外掛程式使用 Gradle 管理的裝置
透過 Firebase Test Lab 外掛程式,Gradle 管理的裝置可讓您根據專案 Gradle 檔案中的設定,在 Test Lab 裝置上大規模執行自動化檢測設備測試。
Gradle 管理的裝置也提供智慧型資料分割功能,可根據先前的測試記錄,以最佳方式將測試分配至各個資料分割。使用智慧資料分割功能時,各個資料分割的執行時間大致相同,且會盡快傳回測試結果。智慧資料分割功能可讓您平行執行大型測試套件,因此非常適合用於 CI/CD 流程。
如要使用 Gradle 管理的裝置 Test Lab 外掛程式啟用智慧型資料分割功能,請按照「使用智慧型資料分割功能,為測試執行程序進行最佳化調整」一文的說明操作。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nFirebase Test Lab provides cloud-based infrastructure for testing Android\napps, and features full integration with Android Studio for running\ninstrumented tests and reviewing test results.\n\nThis guide describes how to modify instrumented tests in Android Studio so you\ncan integrate and run them with Test Lab. For instructions on using\nTest Lab from the Android Studio UI to create a test matrix, run an\ninstrumented test, and view the test results, see\n[Run your tests with Firebase Test Lab](https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#run-ctl).\n\nCapture screenshots\n\nTest Lab provides support for capturing screenshots when running\ninstrumented tests. To learn how to capture screenshots, see\n[Add the screenshot library to your project](/docs/test-lab/android/instrumentation-test#add-screenshot-library).\n\nCreate tests using Espresso Test Recorder\n\nThe Espresso Test Recorder tool lets you create UI tests for your app without\nwriting any test code. You can record your interactions with a device and add\nassertions to verify UI elements in particular snapshots of your app. Espresso\nTest Recorder then takes the saved recording and automatically generates a\ncorresponding Espresso UI test that you can run to test your app in Test Lab.\n\nTo learn more, see\n[Create UI Tests with Espresso Test Recorder](//developer.android.com/studio/test/espresso-test-recorder.html).\n\nModify instrumented test behavior for Test Lab\n\nTest Lab provides a system variable that you can add to your instrumented\ntests so that you can cause them to behave differently when you run them in\nTest Lab than when you run them on your own test device or emulator.\n\nThe following code example reads a system property, `firebase.test.lab`, and\nsets a string, `testLabSetting` to `true` if the test is running in Test Lab.\nThen, it uses the value of this string to control whether additional statements\nare executed: \n\nKotlin \n\n```kotlin\nval testLabSetting = Settings.System.getString(contentResolver, \"firebase.test.lab\")\nif (\"true\" == testLabSetting) {\n // Do something when running in Test Lab\n // ...\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/test-lab/app/src/main/java/com/google/firebase/example/testlab/kotlin/MainActivity.kt#L17-L21\n```\n\nJava \n\n```java\nString testLabSetting = Settings.System.getString(getContentResolver(), \"firebase.test.lab\");\nif (\"true\".equals(testLabSetting)) {\n // Do something when running in Test Lab\n // ...\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/test-lab/app/src/main/java/com/google/firebase/example/testlab/MainActivity.java#L26-L30\n```\n\nUse Gradle Managed Devices via the Firebase Test Lab plugin\n\nGradle Managed Devices via the Firebase Test Lab\nplugin lets you run automated instrumented tests at scale on Test Lab\ndevices, based on the configurations in your project's Gradle files.\n\nGradle Managed Devices also offer smart sharding, which lets you distribute\ntests optimally across shards based on your previous test history. With smart\nsharding, shards run for approximately the same length of time and return test\nresults as quickly as possible. Smart sharding lets you run large test suites in\nparallel, making this feature well suited for CI/CD flows.\n\nTo enable smart sharding using the [Gradle Managed Devices Test Lab plugin](https://developer.android.com/studio/test/gradle-managed-devices#gmd-ftl),\nfollow the instructions in [Optimize test runs with smart\nsharding](https://developer.android.com/studio/test/gradle-managed-devices#smart-sharding)\n."]]