Espresso のセットアップ手順

このガイドでは、SDK Manager を使用して Espresso をインストールし、Gradle を使用して Espresso をビルドする方法について説明します。Android Studio の使用が推奨されます。

テスト環境のセットアップ

不安定性を回避するため、テストに使用する仮想デバイスまたは実機でシステム アニメーションをオフにすることを強くおすすめします。デバイスの [設定] > [開発者向けオプション] で、次の 3 つの設定を無効にします。

  • ウィンドウ アニメ スケール
  • トランジション アニメスケール
  • Animator 再生時間スケール

Espresso の依存関係の追加

Espresso の依存関係をプロジェクトに追加するには、次の手順を行います。

  1. アプリの build.gradle ファイルを開きます。通常、これは最上位の build.gradle ファイルではなく、app/build.gradle です。
  2. 依存関係内に次の行を追加します。

Groovy

androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'

Kotlin

androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0')
androidTestImplementation('androidx.test:runner:1.4.0')
androidTestImplementation('androidx.test:rules:1.4.0')

Gradle のすべての依存関係についてはこちらをご覧ください。

インストゥルメンテーション ランナーの設定

同じ build.gradle ファイルの android.defaultConfig に次の行を追加します。

Groovy

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Kotlin

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Gradle ビルドファイルの例

Groovy

plugins {
  id 'com.android.application'
}

android {
    compileSdkVersion 33

    defaultConfig {
        applicationId "com.my.awesome.app"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Kotlin

plugins {
    id("com.android.application")
}

android {
    compileSdkVersion(33)

    defaultConfig {
        applicationId = "com.my.awesome.app"
        minSdkVersion(21)
        targetSdkVersion(33)
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestImplementation('androidx.test:runner:1.4.0')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0')
}

アナリティクス

新しいリリースがそれぞれ正しい方向に進むように、テストランナーは分析データを収集します。具体的には、テスト対象アプリケーションのパッケージ名のハッシュを呼び出しごとにアップロードします。これにより、Espresso を使用した一意のパッケージの数と使用量の両方を測定できます。

このデータをアップロードしない場合は、インストルメンテーション コマンドに disableAnalytics 引数を含めることでオプトアウトできます。

adb shell am instrument -e disableAnalytics true

最初のテストの追加

Android Studio は、デフォルトでテストを src/androidTest/java/com.example.package/ に作成します。

ルールを使用した JUnit4 テストの例を以下に示します。

Kotlin

@RunWith(AndroidJUnit4::class)
@LargeTest
class HelloWorldEspressoTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Test fun listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()))
    }
}

Java

@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {

    @Rule
    public ActivityScenarioRule<MainActivity> activityRule =
            new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));
    }
}

テストの実行

テストの実行は、Android Studio またはコマンドラインで行えます。

Android Studio での実行

Android Studio でテスト構成を作成するには、次の手順を行います。

  1. [Run] > [Edit Configurations] を開きます。
  2. 新しい Android Tests 構成を追加します。
  3. モジュールを選択します。
  4. 特定のインストゥルメンテーション ランナー(androidx.test.runner.AndroidJUnitRunner)を追加します。
  5. 作成した構成を実行します。

コマンドラインでの実行

次の Gradle コマンドを実行します。

./gradlew connectedAndroidTest