Espresso のセットアップ手順
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このガイドでは、SDK Manager を使用した Espresso のインストール方法と、Gradle を使用した Espresso のビルド方法について説明します。Android Studio の使用が推奨されます。
テスト環境のセットアップ
テストが不安定になるのを避けるため、使用する仮想デバイスまたは実機でシステム アニメーションをオフにすることを強くおすすめします。デバイスの [設定] > [開発者向けオプション] で、次の 3 つの設定を無効にします。
- ウィンドウ アニメスケール
- トランジション アニメスケール
- Animator 再生時間スケール
Espresso の依存関係の追加
Espresso の依存関係をプロジェクトに追加するには、次の手順を行います。
- アプリの
build.gradle
ファイルを開きます。通常、これは最上位の build.gradle
ファイルではなく、app/build.gradle
です。
- 依存関係内に次の行を追加します。
Groovy
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test:rules:1.6.1'
Kotlin
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
androidTestImplementation('androidx.test:runner:1.6.1')
androidTestImplementation('androidx.test:rules:1.6.1')
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.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
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.6.1')
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
}
アナリティクス
新しいリリースのたびに改善が行われるように、テストランナーによって分析情報が収集されます。具体的には、テストランナーが呼び出されるごとに、テスト対象アプリのパッケージ名のハッシュがアップロードされます。これにより Google は、Espresso を使用した一意のパッケージ数と使用量の両方を測定できます。
このデータをアップロードしたくない場合は、次のように instrumentation コマンドに 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 でテスト構成を作成するには、次の手順を行います。
- [Run] > [Edit Configurations] を開きます。
- 新しい Android Tests 構成を追加します。
- モジュールを選択します。
- 特定のインストゥルメンテーション ランナー(
androidx.test.runner.AndroidJUnitRunner
)を追加します。
- 作成した構成を実行します。
コマンドラインでの実行
次の Gradle コマンドを実行します。
./gradlew connectedAndroidTest
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-08-27 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-27 UTC。"],[],[],null,["This guide covers installing Espresso using the SDK Manager and building it\nusing Gradle. Android Studio is recommended.\n\nSet up your test environment\n\nTo avoid flakiness, we highly recommend that you turn off system animations on\nthe virtual or physical devices used for testing. On your device, under\n**Settings \\\u003e Developer options**, disable the following 3 settings:\n\n- Window animation scale\n- Transition animation scale\n- Animator duration scale\n\nAdd Espresso dependencies\n\nTo add Espresso dependencies to your project, complete the following steps:\n\n1. Open your app's `build.gradle` file. This is usually not the top-level `build.gradle` file but `app/build.gradle`.\n2. Add the following lines inside dependencies:\n\nGroovy \n\n```groovy\nandroidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'\nandroidTestImplementation 'androidx.test:runner:1.6.1'\nandroidTestImplementation 'androidx.test:rules:1.6.1'\n```\n\nKotlin \n\n```kotlin\nandroidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')\nandroidTestImplementation('androidx.test:runner:1.6.1')\nandroidTestImplementation('androidx.test:rules:1.6.1')\n```\n\n[View the complete set of Gradle dependencies](/studio/build/dependencies).\n\nSet the instrumentation runner\n\nAdd to the same `build.gradle` file the following line in\n`android.defaultConfig`: \n\nGroovy \n\n```groovy\ntestInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n```\n\nKotlin \n\n```kotlin\ntestInstrumentationRunner = \"androidx.test.runner.AndroidJUnitRunner\"\n```\n\nExample Gradle build file \n\nGroovy \n\n```groovy\nplugins {\n id 'com.android.application'\n}\n\nandroid {\n compileSdkVersion 33\n\n defaultConfig {\n applicationId \"com.my.awesome.app\"\n minSdkVersion 21\n targetSdkVersion 33\n versionCode 1\n versionName \"1.0\"\n\n testInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n }\n}\n\ndependencies {\n androidTestImplementation 'androidx.test:runner:1.6.1'\n androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'\n}\n```\n\nKotlin \n\n```kotlin\nplugins {\n id(\"com.android.application\")\n}\n\nandroid {\n compileSdkVersion(33)\n\n defaultConfig {\n applicationId = \"com.my.awesome.app\"\n minSdkVersion(21)\n targetSdkVersion(33)\n versionCode = 1\n versionName = \"1.0\"\n\n testInstrumentationRunner = \"androidx.test.runner.AndroidJUnitRunner\"\n }\n}\n\ndependencies {\n androidTestImplementation('androidx.test:runner:1.6.1')\n androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')\n}\n```\n\nAnalytics\n\nIn order to make sure we are on the right track with each new release, the test\nrunner collects analytics. More specifically, it uploads a hash of the package\nname of the application under test for each invocation. This allows us to\nmeasure both the count of unique packages using Espresso as well as the volume\nof usage.\n\nIf you do not wish to upload this data, you can opt out by including the\n`disableAnalytics` argument in your instrumentation command: \n\n```bash\nadb shell am instrument -e disableAnalytics true\n```\n\nAdd the first test\n\nAndroid Studio creates tests by default in\n`src/androidTest/java/com.example.package/`.\n\nExample JUnit4 test using Rules: \n\nKotlin \n\n```kotlin\n@RunWith(AndroidJUnit4::class)\n@LargeTest\nclass HelloWorldEspressoTest {\n\n @get:Rule\n val activityRule = ActivityScenarioRule(MainActivity::class.java)\n\n @Test fun listGoesOverTheFold() {\n onView(withText(\"Hello world!\")).check(matches(isDisplayed()))\n }\n}\n```\n\nJava \n\n```java\n@RunWith(AndroidJUnit4.class)\n@LargeTest\npublic class HelloWorldEspressoTest {\n\n @Rule\n public ActivityScenarioRule\u003cMainActivity\u003e activityRule =\n new ActivityScenarioRule\u003c\u003e(MainActivity.class);\n\n @Test\n public void listGoesOverTheFold() {\n onView(withText(\"Hello world!\")).check(matches(isDisplayed()));\n }\n}\n```\n\nRun tests\n\nYou can run your tests in Android Studio or from the command line.\n\nIn Android Studio\n\nTo create a test configuration in Android Studio, complete the following steps:\n\n1. Open **Run \\\u003e Edit Configurations**.\n2. Add a new Android Tests configuration.\n3. Choose a module.\n4. Add a specific instrumentation runner: `androidx.test.runner.AndroidJUnitRunner`\n5. Run the newly created configuration.\n\nFrom the command line\n\nExecute the following Gradle command: \n\n```bash\n./gradlew connectedAndroidTest\n```"]]