Android Studio集成Cucumber+Espresso失败及提示无法关闭问题
Android中Cucumber+Espresso集成失败及配置无效问题解决
你的集成失败和配置无效问题主要来自依赖版本冲突、配置文件路径错误、Runner类配置不规范三个核心问题,以下是针对性的修复方案:
1. 清理依赖:解决版本冲突与冗余
你的依赖中同时混用了已停止维护的旧版Cucumber(info.cukes系列,1.2.x)和新版Cucumber(io.cucumber系列,6.6.0),这两类包完全不兼容,是导致集成失败的核心原因。同时存在大量重复依赖(如JUnit、Espresso),需要彻底清理:
修正后的模块build.gradle:
plugins { id 'com.android.application' } android { compileSdk 31 defaultConfig { applicationId "com.example.loginexample" minSdk 22 targetSdk 31 versionCode 1 versionName "1.0" testApplicationId "com.example.loginexample.tests" // 使用Cucumber官方的Instrumentation Runner,无需自定义 testInstrumentationRunner "io.cucumber.android.runner.CucumberAndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { // 主项目依赖 implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' // 本地单元测试依赖 testImplementation 'junit:junit:4.13.2' testImplementation 'org.mockito:mockito-core:4.5.1' // 补全版本号 testImplementation 'org.powermock:powermock-api-mockito2:2.0.9' testImplementation 'org.powermock:powermock-module-junit4:2.0.9' // Instrumented测试(Cucumber+Espresso)依赖 androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0' { exclude module: 'support-annotations' exclude module: 'support-v4' exclude module: 'recyclerview-v7' } androidTestImplementation 'org.hamcrest:hamcrest-library:2.2' // 统一使用新版Cucumber Android依赖 androidTestImplementation 'io.cucumber:cucumber-android:6.10.4' androidTestImplementation 'io.cucumber:cucumber-picocontainer:6.10.4' }
关键说明:
- 移除所有
info.cukes和冗余的io.cucumber依赖,统一使用官方维护的io.cucumber:cucumber-android包 - 补全Mockito版本号,避免依赖解析报错
- 替换自定义Instrumentation为Cucumber官方的
CucumberAndroidJUnitRunner,简化配置
2. 修正Cucumber配置文件位置
你创建的cucumber.properties放在了src/test/resources,但这是本地单元测试的资源目录,而Cucumber+Espresso属于Instrumented测试,需要将文件移动到src/androidTest/resources目录下,内容保持不变:
cucumber.publish.quiet=true
3. 规范Runner类配置
你的Runner类存在包名不匹配、未继承正确父类的问题,修正后的Runner类:
package com.example.loginexample.tests; // 与testApplicationId保持一致 import io.cucumber.junit.CucumberOptions; import io.cucumber.android.runner.CucumberAndroidJUnitRunner; @CucumberOptions( plugin = { "pretty", "html:cucumber-reports/html-report", "json:cucumber-reports/report.json", "junit:cucumber-reports/report.xml" }, features = "@android/assets/features", // Android中识别assets目录的标准格式 tags = "@shortSanity" ) public class CucumberTestRunner extends CucumberAndroidJUnitRunner { // 无需自定义REPORT_PATH,Cucumber会自动处理测试报告路径 }
关键说明:
- 包名改为
com.example.loginexample.tests,与defaultConfig中的testApplicationId一致 - 继承
CucumberAndroidJUnitRunner,让Android识别这是Instrumented测试的Runner - 修改
features路径为@android/assets/features,这是Cucumber Android识别assets目录的标准写法 - 简化报告路径,避免硬编码系统路径,Cucumber会自动在测试目录生成报告
最终修复步骤
- 删除旧的
src/test/resources/cucumber.properties,在src/androidTest/resources下重新创建 - 替换模块
build.gradle为修正后的代码,同步项目 - 替换Runner类为修正后的代码
- 确保
assets/features目录存在且包含正确的.feature文件
内容的提问来源于stack exchange,提问作者Zeynep Turhan
相关产品推荐
相关产品推荐

