Compose レイアウトをテストする
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
アプリの UI をテストして、Compose コードの動作が正しいことを確認します。これにより、エラーを早期に検出し、アプリの品質を向上させることができます。
Compose には、要素を検索し、属性を検証して、ユーザー アクションを実行するためのテスト API が用意されています。また、時間操作などの高度な機能も含まれます。これらの API を使用して、アプリの動作を検証する堅牢なテストを作成します。
View
Compose ではなくビューを使用している場合は、一般的な Android でアプリをテストするセクションをご覧ください。
特に、UI テストを自動化するガイドから始めることをおすすめします。このガイドでは、ビューを使用する場合など、デバイス上で実行されるテストを自動化する方法について説明します。
主な概念
Compose コードのテストに関する主なコンセプトは次のとおりです。
- セマンティクス: セマンティクスは UI に意味を与え、テストで特定の要素を操作できるようにします。
- テスト API: テスト API を使用すると、要素を検索し、属性を検証して、ユーザー アクションを実行できます。
- 同期: 同期は、テストがアクションの実行やアサーションの作成の前に UI がアイドル状態になるのを待機することを検証します。
- 相互運用性: 相互運用性により、テストは同じアプリ内の Compose ベースの要素と View ベースの要素の両方で動作できます。
テスト早見表
Compose でのテストについて学ぶべきすべての主要なトピックの概要については、テスト クイック リファレンスをご覧ください。
設定
Compose コードをテストできるようにアプリを設定します。
まず、UI テストを含むモジュールの build.gradle
ファイルに次の依存関係を追加します。
// Test rules and transitive dependencies:
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
// Needed for createComposeRule(), but not for createAndroidComposeRule<YourActivity>():
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
このモジュールには、ComposeTestRule
と、AndroidComposeTestRule
と呼ばれる Android 用の実装が含まれています。このルールを通じて、Compose コンテンツを設定したり、アクティビティにアクセスしたりできます。ルールは、ファクトリー関数 createComposeRule
または(アクティビティへのアクセスが必要な場合は)createAndroidComposeRule
を使用して構築します。Compose の一般的な UI テストは次のようになります。
// file: app/src/androidTest/java/com/package/MyComposeTest.kt
class MyComposeTest {
@get:Rule val composeTestRule = createComposeRule()
// use createAndroidComposeRule<YourActivity>() if you need access to
// an activity
@Test
fun myTest() {
// Start the app
composeTestRule.setContent {
MyAppTheme {
MainScreen(uiState = fakeUiState, /*...*/)
}
}
composeTestRule.onNodeWithText("Continue").performClick()
composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
}
}
参考情報
- Android でアプリをテストする: Android テストのメイン ランディング ページでは、テストの基礎と手法についてより広範な視点から説明しています。
- テストの基礎: Android アプリのテストの背景にある基本概念について詳しく学びます。
- ローカルテスト: 一部のテストは、自分のワークステーションでローカルに実行できます。
- インストゥルメント化テスト: インストゥルメント化テストも実行することをおすすめします。つまり、デバイス上で直接実行されるテストです。
- 継続的インテグレーション:
継続的インテグレーションを使用すると、テストをデプロイ パイプラインに統合できます。
- さまざまな画面サイズをテストする: ユーザーが利用できるデバイスは多種多様であるため、さまざまな画面サイズでテストする必要があります。
- Espresso: View ベースの UI を対象としていますが、Espresso の知識は Compose テストのいくつかの側面で役立ちます。
Codelab
詳細については、Jetpack Compose テスト Codelab をご覧ください。
サンプル
あなたへのおすすめ
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-08-21 UTC。
[[["わかりやすい","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-08-21 UTC。"],[],[],null,["# Test your Compose layout\n\nTest your app's UI to verify that behavior of your Compose code is\ncorrect. This lets you catch errors early and improve the quality of your app.\n\nCompose provides a set of testing APIs to find elements, verify their\nattributes, and perform user actions. The APIs also include advanced features\nsuch as time manipulation. Use these APIs to create robust tests that verify\nyour app's behavior.\n| **Important:** For more on testing Android apps in general, including testing `View` elements, see the [general testing section](/training/testing). A good place to start is the [Fundamentals of testing Android apps](/training/testing/fundamentals) guide.\n\nViews\n-----\n\nIf you are working with views instead of Compose, see the general [Test apps on\nAndroid](/training/testing) section.\n\nIn particular, a good place to start is the [Automate UI tests](/training/testing/ui-tests) guide. It\nlays out how you can automate tests that run on-device, including when using\nviews.\n\nKey Concepts\n------------\n\nThe following are some key concepts for testing your Compose code:\n\n- **[Semantics](/develop/ui/compose/testing/semantics)**: Semantics give meaning to your UI, allowing tests to interact with specific elements.\n- **[Testing APIs](/develop/ui/compose/testing/apis)**: Testing APIs let you find elements, verify their attributes, and perform user actions.\n- **[Synchronization](/develop/ui/compose/testing/synchronization)**: Synchronization verifies that tests wait for the UI to be idle before performing actions or making assertions.\n- **[Interoperability](/develop/ui/compose/testing/interoperability)**: Interoperability enables tests to work with both Compose and View-based elements in the same app.\n\nTesting cheatsheet\n------------------\n\nSee the [testing cheatsheet](/develop/ui/compose/testing/testing-cheatsheet) for an overview of all the key topics you should\nlearn about testing in Compose.\n\nSetup\n-----\n\nSet up your app to let you test compose code.\n\nFirst, add the following dependencies to the `build.gradle` file of the module\ncontaining your UI tests: \n\n // Test rules and transitive dependencies:\n androidTestImplementation(\"androidx.compose.ui:ui-test-junit4:$compose_version\")\n // Needed for createComposeRule(), but not for createAndroidComposeRule\u003cYourActivity\u003e():\n debugImplementation(\"androidx.compose.ui:ui-test-manifest:$compose_version\")\n\nThis module includes a [`ComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule) and an implementation for Android\ncalled [`AndroidComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule). Through this rule you can set Compose\ncontent or access the activity. You construct the rules using factory functions,\neither [`createComposeRule`](/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule()) or, if you need access to an activity,\n[`createAndroidComposeRule`](/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule()). A typical UI test for Compose looks like this: \n\n // file: app/src/androidTest/java/com/package/MyComposeTest.kt\n\n class MyComposeTest {\n\n @get:Rule val composeTestRule = createComposeRule()\n // use createAndroidComposeRule\u003cYourActivity\u003e() if you need access to\n // an activity\n\n @Test\n fun myTest() {\n // Start the app\n composeTestRule.setContent {\n MyAppTheme {\n MainScreen(uiState = fakeUiState, /*...*/)\n }\n }\n\n composeTestRule.onNodeWithText(\"Continue\").performClick()\n\n composeTestRule.onNodeWithText(\"Welcome\").assertIsDisplayed()\n }\n }\n\nAdditional Resources\n--------------------\n\n- **[Test apps on Android](/training/testing)**: The main Android testing landing page provides a broader view of testing fundamentals and techniques.\n- **[Fundamentals of testing](/training/testing/fundamentals):** Learn more about the core concepts behind testing an Android app.\n- **[Local tests](/training/testing/local-tests):** You can run some tests locally, on your own workstation.\n- **[Instrumented tests](/training/testing/instrumented-tests):** It is good practice to also run instrumented tests. That is, tests that run directly on-device.\n- **[Continuous integration](/training/testing/continuous-integration):** Continuous integration lets you integrate your tests into your deployment pipeline.\n- **[Test different screen sizes](/training/testing/different-screens):** With some many devices available to users, you should test for different screen sizes.\n- **[Espresso](/training/testing/espresso)**: While intended for View-based UIs, Espresso knowledge can still be helpful for some aspects of Compose testing.\n\nCodelab\n-------\n\nTo learn more, try the [Jetpack Compose Testing codelab](/codelabs/jetpack-compose-testing).\n\n### Samples\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Semantics in Compose](/develop/ui/compose/semantics)\n- [Window insets in Compose](/develop/ui/compose/layouts/insets)\n- [Other considerations](/develop/ui/compose/migrate/other-considerations)"]]