アプリの UI をテストして、Compose コードの動作が正しいことを確認します。これにより、エラーを早期に発見してアプリの品質を改善できます。
Compose には、要素を検索し、属性を検証して、ユーザー アクションを実行するためのテスト API が用意されています。また、時間操作などの高度な機能も含まれます。これらの API を使用して、アプリの動作を確認する堅牢なテストを作成します。
View
Compose ではなくビューを使用している場合は、一般的なAndroid でアプリをテストするをご覧ください。
特に、UI テストを自動化するガイドを参照することをおすすめします。ビューの使用時など、デバイス上で実行されるテストを自動化する方法について説明します。
主な概念
Compose コードをテストするための主なコンセプトは次のとおりです。
- セマンティクス: Compose テストは、UI の一部に意味を与え、UI 階層とともに生成されるセマンティクスを使用して UI を操作します。
- テスト API: Compose には、要素の検索、状態とプロパティの断言、アクションの実行によるユーザー操作のシミュレーションを行うテスト API が用意されています。
- 同期: デフォルトでは、Compose テストは UI と自動的に同期され、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 をご覧ください。
サンプル
Founded in 2017, FoxNext Games was created to expand the universes of cherished franchises like The Simpsons, Alien, Ice Age, and Avatar through interactive and immersive next-generation storytelling. With over 50 million reported installs and more than 500,000 five-star ratings, Galaxy Attack is a popular alien shooter game. With more than a million app customers, Evino is an innovative online wine business based in Brazil. With chords and tabs for over 1.1 million songs, Ultimate Guitar (UG) is one of the fastest growing guitarist communities in the world. Rakuten Viki, home of Viki Originals and the annual Soompi Awards, is a premier destination for multicultural youth entertainment and the worlds leading platform for Asian TV shows, movies, and influencer videos – translated into 200+ languages by a community of avid fans. King is a leading interactive entertainment company, with popular mobile games such as Candy Crush Saga, Farm Heroes Saga and Bubble Witch 3 Saga. Jam City is a leading developer in social gaming and the maker of Panda Pop (built with Unity), a top-grossing bubble shooter game with the goal to rescue baby pandas trapped in bubbles! Vimeo was founded by a group of filmmakers who wanted to share their creative work and personal moments from their lives.Marvel Strike Force and Google Play Instant join forces to improve install rate
OneSoft uses instant apps to boost Galaxy Attack install rate by 15%
Evino attracts new customers with Google Play Instant
Ultimate Guitar takes advantage of 10MB limit for instant apps and improves install rate by 8%
Rakuten Viki increases watch time 5x with Google Play Instant
King improves player acquisition with Google Play Instant
Panda Pop finds high-quality players with instant apps
Vimeo increases session duration by 130% with instant apps
現在、おすすめはありません。
Google アカウントにログインしてください。