Android Studio でテストを実行する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firebase Test Lab は、Android アプリをテストするためのクラウドベースのインフラストラクチャです。Android Studio と完全に連携しており、インストルメンテーション テストの実行とテスト結果の確認を行えます。
このガイドでは、Test Lab と統合してテストを実行するために、Android Studio でインストルメンテーション テストを変更する方法について説明します。Android Studio の UI から Test Lab を使用して、テスト マトリックスの作成、インストルメンテーション テストの実行、テスト結果の確認を行う手順については、Firebase Test Lab でテストを実行するをご覧ください。
スクリーンショットをキャプチャする
Test Lab は、インストルメンテーション テストの実行時にスクリーンショットをキャプチャできます。スクリーンショットをキャプチャする方法については、プロジェクトにスクリーンショット ライブラリを追加するをご覧ください。
Espresso テスト レコーダーを使用してテストを作成する
Espresso テスト レコーダー ツールを使用すると、テストコードを記述しなくてもアプリの UI テストを作成できます。デバイスでの操作を記録し、アサーションを追加して、アプリの特定のスナップショットの UI 要素を検証できます。Espresso テスト レコーダーは、保存された記録を受け取り、対応する Espresso UI テストを自動的に生成します。生成されたテストは Test Lab でアプリをテストする際に使用できます。
詳しくは、Espresso テスト レコーダーで 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 プラグインを使用してスマート シャーディングを有効にするには、スマート シャーディングでテスト実行を最適化するの手順を実施してください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-21 UTC。
[null,null,["最終更新日 2025-08-21 UTC。"],[],[],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."]]