アプリの UI をテストして、Compose コードの動作が正しいことを確認します。これにより、エラーを早期に発見してアプリの品質を改善できます。
Compose には、要素を検索し、属性を検証して、ユーザー アクションを実行するためのテスト API が用意されています。また、時間操作などの高度な機能も含まれます。これらの API を使用して、アプリの動作を確認する堅牢なテストを作成します。
View
Compose ではなくビューを使用している場合は、一般的なAndroid でアプリをテストするをご覧ください。
特に、UI テストを自動化するガイドを参照することをおすすめします。ビューの使用時など、デバイス上で実行されるテストを自動化する方法について説明します。
主な概念
Compose コードをテストするための主なコンセプトは次のとおりです。
- セマンティクス: セマンティクスは UI に意味を与え、テストが特定の要素を操作できるようにします。
- テスト API: テスト API を使用すると、要素を検索し、属性を検証して、ユーザー アクションを実行できます。
- 同期: 同期では、テストがアクションを実行またはアサーションを実行する前に UI がアイドル状態になるのを待機することを検証します。
- 相互運用性: 相互運用性により、同じアプリ内で Compose ベースの要素とビューベースの要素の両方をテストできます。
テスト早見表
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: Espresso はビューベースの UI を対象としていますが、Compose テストの一部のアスペクトでも役立ちます。
Codelab
詳細については、Jetpack Compose テストの Codelab をご覧ください。
サンプル
Jetchat is a sample chat app built with Jetpack Compose.
To try out this sample app, use the latest stable version of Android Studio. You can clone this repository or import the project from Android Studio following the steps here.
This sample Jetnews is a sample news reading app, built with Jetpack Compose. The goal of the sample is to showcase the current UI capabilities of Compose.
To try out this sample app, use the latest stable version of Android Studio. You can clone this repository Learn how this app was designed and built in the design case study, architecture learning journey and modularization learning journey.
This is the repository for the Now in Android app. It is a work in progress 🚧.
Now in Android is a fully functionalJetchat sample
Jetnews sample
Now in Android App