Test Lab 提供系統變數,您可以將該變數新增至插碼測試,以便在 Test Lab 中執行測試時,讓測試的行為與在自己的測試裝置或模擬器上執行測試時不同。
下列程式碼範例會讀取系統屬性 firebase.test.lab,並在測試於 Test Lab 中執行時,將字串 testLabSetting 設為 true。然後,系統會使用這個字串的值,控管是否執行其他陳述式:
Kotlin
valtestLabSetting=Settings.System.getString(contentResolver,"firebase.test.lab")if("true"==testLabSetting){// Do something when running in Test Lab// ...}
StringtestLabSetting=Settings.System.getString(getContentResolver(),"firebase.test.lab");if("true".equals(testLabSetting)){// Do something when running in Test Lab// ...}
[[["容易理解","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,["\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/b694d4dbd411d31be39655f47691c3e9f3529b03/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/b694d4dbd411d31be39655f47691c3e9f3529b03/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."]]