升级espresso-contrib至3.3.0以上版本导致构建失败求助
我们配置androidTestImplementation("androidx.test.espresso:espresso-contrib:3.3.0")时,Instrumentation测试可正常运行。但升级至3.4.0或3.5.0等新版本时,出现以下构建失败错误:
Instrumentation did not complete: Instrumentation code: 0 Instrumentation result bundle: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:connectedDevShortTimeoutDebugAndroidTest'. > There were failing tests. See the report at:
测试报告显示无测试用例被找到。当前使用的androidTestImplementation依赖如下:
androidTestImplementation(project(":sharedTest")) androidTestImplementation("androidx.test:core-ktx:1.5.0") androidTestImplementation("androidx.test:rules:1.5.0") androidTestImplementation("androidx.test:runner:1.5.0") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0") androidTestImplementation("androidx.test.ext:junit-ktx:1.1.4") androidTestImplementation("androidx.test.espresso:espresso-contrib:3.3.0") androidTestImplementation("com.microsoft.appcenter:espresso-test-extension:1.5") androidTestImplementation("org.mockito:mockito-android:$mockitoTestVersion") androidTestImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0") androidTestUtil("androidx.test:orchestrator:1.4.2")
解决思路与方案
统一Espresso依赖版本:当前
espresso-core为3.5.0,但espresso-contrib仍停留在3.3.0,版本不一致会引发兼容性问题。将espresso-contrib升级至3.5.0,与核心库版本对齐:androidTestImplementation("androidx.test.espresso:espresso-contrib:3.5.0")验证测试类配置:确保测试类放置在
androidTest目录下,类上标注正确的运行注解(如@RunWith(AndroidJUnit4::class)),测试方法添加@Test注解,避免因注解缺失导致测试用例未被识别。同步Orchestrator版本:当前
orchestrator版本为1.4.2,与androidx.test:runner:1.5.0存在版本不匹配风险,升级至1.5.0:androidTestUtil("androidx.test:orchestrator:1.5.0")排查第三方扩展冲突:
com.microsoft.appcenter:espresso-test-extension:1.5版本较旧,可能与新版Espresso不兼容。尝试升级该依赖至最新版本,或暂时移除后重新运行测试,确认是否为冲突根源。清理构建缓存:执行
./gradlew clean清除旧构建缓存,再重新执行测试任务,避免缓存导致的依赖加载异常。检查测试过滤规则:检查
build.gradle中是否存在错误的测试过滤配置,确保没有意外排除所有测试类的规则。
内容的提问来源于stack exchange,提问作者Aaron

