Compose は一般的なテスト フレームワークと統合されています。
Espresso との相互運用
ハイブリッド アプリでは、ビュー階層内に Compose コンポーネントがあり、
Compose コンポーザブル内のビュー(AndroidView
コンポーザブルを使用)
どちらのタイプも、マッチングするために特別な手順は必要ありません。視聴回数のマッチ率
Espresso の onView
、ComposeTestRule
を持つ Compose 要素。
@Test
fun androidViewInteropTest() {
// Check the initial state of a TextView that depends on a Compose state.
Espresso.onView(withText("Hello Views")).check(matches(isDisplayed()))
// Click on the Compose button that changes the state.
composeTestRule.onNodeWithText("Click here").performClick()
// Check the new value.
Espresso.onView(withText("Hello Compose")).check(matches(isDisplayed()))
}
UiAutomator との相互運用
デフォルトでは、コンポーザブルに UiAutomator からアクセスできるのは、
わかりやすい記述子(表示テキスト、コンテンツの説明など)を付ける必要があります。目標
Modifier.testTag
を使用するコンポーザブルにアクセスするには、以下を有効にする必要があります。
特定のコンポーザブルのセマンティック プロパティ testTagsAsResourceId
サブツリーです。この動作を有効にすると、何も持たないコンポーザブルで
スクロール可能なコンポーザブル(LazyColumn
など)など、その他の一意のハンドル。
コンポーザブルの階層の上位で 1 回だけセマンティック プロパティを有効にして、
Modifier.testTag
を持つネストされたすべてのコンポーザブルが、
UiAutomator。
Scaffold(
// Enables for all composables in the hierarchy.
modifier = Modifier.semantics {
testTagsAsResourceId = true
}
){
// Modifier.testTag is accessible from UiAutomator for composables nested here.
LazyColumn(
modifier = Modifier.testTag("myLazyColumn")
){
// Content
}
}
Modifier.testTag(tag)
を持つコンポーザブルは、次のコマンドでアクセスできます。
の By.res(resourceName)
。resourceName
と同じ tag
を使用します。
val device = UiDevice.getInstance(getInstrumentation())
val lazyColumn: UiObject2 = device.findObject(By.res("myLazyColumn"))
// Some interaction with the lazyColumn.
参考情報
- Android でアプリをテストする: 主な Android テスト テストの基礎と手法を幅広く把握できる。
- テストの基礎: 詳細 Android アプリのテストの基本コンセプトについて説明します。
- ローカルテスト: いくつかのテストを実行できます 独自のワークステーションに ローカルに作成します
- インストルメンテーション テスト: インストルメンテーション テストも実行します。つまり、直接実行されるテストは、 できます。
- 継続的インテグレーション: 継続的インテグレーションにより、テストをデプロイに統合できる 説明します
- さまざまな画面サイズをテストする: 利用可能であれば、さまざまな画面でテストする必要があります。 あります。
- Espresso: ビューベース向け UI や Espresso の知識は Compose のいくつかの側面で引き続き役立ちます 説明します。