Espresso 设置说明
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南介绍如何使用 SDK 管理器安装 Espresso 以及如何使用 Gradle 构建 Espresso 测试。建议您使用 Android Studio。
设置测试环境
为了避免测试不稳定,我们强烈建议您在用于测试的虚拟或物理设备上关闭系统动画。在您的设备上,在设置 > 开发者选项下,停用以下三项设置:
- 窗口动画缩放
- 过渡动画缩放
- 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')
}
分析
为了确保每个新版本都在正确的轨道上,测试运行程序会收集分析数据。更具体地说,它会针对每次调用上传被测应用的软件包名称的哈希值。这样,我们就可以使用 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 中创建测试配置,请完成以下步骤:
- 依次打开 Run > Edit Configurations。
- 添加新的 Android Tests 配置。
- 选择一个模块。
- 添加特定的插桩测试运行程序:
androidx.test.runner.AndroidJUnitRunner
- 运行新创建的配置。
从命令行
执行以下 Gradle 命令:
./gradlew connectedAndroidTest
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Espresso setup instructions\n\nThis guide covers installing Espresso using the SDK Manager and building it\nusing Gradle. Android Studio is recommended.\n\nSet up your test environment\n----------------------------\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-------------------------\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\n### Groovy\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\n### Kotlin\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------------------------------\n\nAdd to the same `build.gradle` file the following line in\n`android.defaultConfig`: \n\n### Groovy\n\n```groovy\ntestInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n```\n\n### Kotlin\n\n```kotlin\ntestInstrumentationRunner = \"androidx.test.runner.AndroidJUnitRunner\"\n```\n\nExample Gradle build file\n-------------------------\n\n### Groovy\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\n### Kotlin\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---------\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------------------\n\nAndroid Studio creates tests by default in\n`src/androidTest/java/com.example.package/`.\n\nExample JUnit4 test using Rules: \n\n### Kotlin\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\n### Java\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---------\n\nYou can run your tests in Android Studio or from the command line.\n\n### In 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\n### From the command line\n\nExecute the following Gradle command: \n\n```bash\n./gradlew connectedAndroidTest\n```"]]