2. 搭建 Espresso 测试项目:Gradle 依赖配置、测试目录结构、AndroidJUnitRunner 设置
本章聚焦于在 Android 项目中高效配置 Espresso 测试环境。正确的 Gradle 依赖、测试目录结构和 Instrumentation Runner 设置是自动化测试稳定运行的基础。本节将依次讲解每个环节的配置要点、可能出现的陷阱以及调试方法。
2.1 Gradle 依赖配置
Espresso 测试依赖声明在模块级 build.gradle(或 build.gradle.kts)的 androidTestImplementation 作用域中。核心组件与版本对齐规则如下表所示:
| 依赖库 | 作用域 | 最低版本建议 | 说明 |
|---|---|---|---|
| androidx.test:core | androidTestImplementation | 1.5.0 | 测试核心 API,如 ApplicationProvider |
| androidx.test:runner | androidTestImplementation | 1.5.2 | AndroidJUnitRunner 支持 |
| androidx.test:rules | androidTestImplementation | 1.5.0 | ActivityScenarioRule 等测试规则 |
| androidx.test.espresso:espresso-core | androidTestImplementation | 3.5.1 | Espresso 核心 View 匹配与交互 |
| androidx.test.espresso:espresso-contrib | androidTestImplementation | 3.5.1 | RecyclerView、AdapterView 等扩展 |
| androidx.compose.ui:ui-test-junit4 | androidTestImplementation | 对应 Compose BOM | Compose 测试 API(本课程重点) |
版本冲突陷阱:Espresso 与 Compose 测试库需使用相同版本的
androidx.test 基础库。推荐通过 BOM 统一管理:
// build.gradle
dependencies {
implementation platform('androidx.compose:compose-bom:2024.02.00')
androidTestImplementation platform('androidx.compose:compose-bom:2024.02.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test:rules:1.5.0'
}
2.2 测试目录结构
Android 测试源集分为单元测试(test/)和仪器化测试(androidTest/)。Espresso 与 Compose UI 测试属于仪器化测试,必须放在 androidTest/ 目录下。典型结构如下:
app/
├── src/
│ ├── main/
│ │ ├── java/com/example/app/ (生产代码)
│ │ └── AndroidManifest.xml
│ ├── test/ (单元测试,不涉及UI)
│ │ └── java/com/example/app/ExampleUnitTest.kt
│ └── androidTest/ (仪器化测试,含Espresso/Compose)
│ └── java/com/example/app/
│ ├── MainActivityTest.kt
│ ├── ComposeRefreshTest.kt
│ └── ...
└── build.gradle
命名约定:测试类名建议以
Test 结尾(如 MainActivityTest),并放置在与应用包名相同的包路径下。AndroidJUnitRunner 默认扫描 androidTest/ 下所有以 Test 结尾的类。
2.3 AndroidJUnitRunner 设置
在 build.gradle 的 defaultConfig 块中,显式指定 Instrumentation Runner:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// 如果使用参数化测试或自定义 Runner,可替换为其他实现
}
}
若项目使用 Compose,还需确保 ui-test-junit4 库中自带的 ComposeTestRule 与 Runner 协同工作。AndroidJUnitRunner 负责将测试注入到 Instrumentation 环境,并管理各个测试规则(如 ActivityScenarioRule、ComposeTestRule)的生命周期。
2.4 完整 build.gradle 配置示例
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.app'
compileSdk 34
defaultConfig {
applicationId "com.example.app"
minSdk 24
targetSdk 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.5.8'
}
}
dependencies {
// 生产依赖
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.compose.ui:ui:1.6.0'
implementation 'androidx.compose.material3:material3:1.2.0'
// 测试依赖
androidTestImplementation platform('androidx.compose:compose-bom:2024.02.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
}
2.5 常见错误与调试
- Runner 未找到:在
defaultConfig中遗漏testInstrumentationRunner或拼写错误。Logcat 会输出No test runner found。 - 版本冲突:Espresso 与 Compose 测试库依赖不同版本的
androidx.test基础库。使用 BOM 或统一版本号可解决。 - 测试类不在 androidTest 目录:Android Studio 默认将单元测试放在
test/,若误将 Espresso 测试放入test/,运行时会提示No tests found或NullPointerException(无法找到 Context)。 - Compose 测试规则初始化失败:确保
androidTestImplementation中已添加ui-test-junit4,且 Compose BOM 版本与生产环境匹配。
调试命令:使用 Gradle 任务清单检查 Runner:
# 查看所有已配置的测试任务
./gradlew tasks --group verification
# 执行仪器化测试(带日志)
./gradlew :app:connectedDebugAndroidTest --info
2.6 项目结构流程图
2.7 验证配置
执行以下命令确认环境就绪:
# 在 Android Studio 中
# 右键点击 androidTest 目录中的任意测试类 -> Run
# 命令行执行全部仪器化测试
./gradlew :app:connectedDebugAndroidTest
# 查看测试 Runner 启动日志
adb logcat | grep -i "TestRunner"