使用 Android Studio 运行测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Firebase Test Lab 提供了用于测试 Android 应用的云基础架构,它与 Android Studio 完全集成,让您可以轻松运行插桩测试并查看测试结果。
本指南介绍如何在 Android Studio 中修改插桩测试,以便将其与 Test Lab 集成并运行这些测试。如需了解如何通过 Android Studio 界面使用 Test Lab 来创建测试矩阵,运行插桩测试,以及查看测试结果,请参阅使用 Firebase Test Lab 运行测试。
捕捉屏幕截图
Test Lab 支持在运行插桩测试时捕捉屏幕截图。要了解如何捕捉屏幕截图,请参阅将屏幕截图库添加到项目。
使用 Espresso 测试记录器创建测试
借助 Espresso 测试记录器工具,您可以为自己的应用创建界面测试,而无需编写任何测试代码。您可以记录自己与设备的交互,并添加断言来验证应用的特定快照中的界面元素。然后,Espresso 测试记录器会根据保存的记录自动生成 Espresso 界面测试。您可以在 Test Lab 中运行该测试来测试您的应用。
如需了解详情,请参阅使用 Espresso 测试记录器创建界面测试。
修改 Test Lab 的插桩测试行为
Test Lab 提供了一个系统变量,您可以将其添加到插桩测试中,以使这些测试在 Test Lab 中运行时,其行为方式与在您自己的测试设备或模拟器上运行时不同。
以下代码示例会读取系统属性 firebase.test.lab
,并将字符串 testLabSetting
设置为 true
(如果测试在 Test Lab 中运行)。随后,它会使用此字符串的值来控制是否执行其他语句:
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 管理的设备
借助 Gradle 通过 Firebase Test Lab 插件管理的设备,您可以根据项目的 Gradle 文件中的配置,在 Test Lab 设备上运行大规模的自动化插桩测试。
Gradle 管理的设备还提供智能分片功能,可让您根据之前的测试记录,实现理想的测试分布。借助智能分片功能,分片的运行时间大致相同,而且系统会尽快返回测试结果。借助智能分片功能,您可以并行运行大型测试套件,这一点使得此功能非常适合 CI/CD 流程。
如需使用 Gradle 管理的设备 Test Lab 插件启用智能分片功能,请按照利用智能分片优化测试运行中的说明操作。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):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."]]