You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Action中Espresso测试报NoActivityResumedException失败求助

解决GitHub Action中Espresso Fragment测试的NoActivityResumedException问题

针对本地测试正常、GitHub Action上MapFragment测试全部报NoActivityResumedException的问题,可从以下几个方向排查修复:

1. 修正CI测试执行命令

当前GitHub Action脚本执行的createDebugAndroidReport仅生成构建报告,不会运行插桩测试。必须替换为插桩测试的执行命令:

./gradlew connectedDebugAndroidTest

若需同时生成测试报告,可合并任务:

./gradlew connectedDebugAndroidTest createDebugAndroidReport

2. 优化模拟器启动配置

CI环境的模拟器资源有限,不稳定的启动参数会导致Fragment生命周期异常:

  • 开启disable-animations:动画会干扰Espresso的元素状态判断
  • 添加模拟器启动参数,提升稳定性与启动速度
    修改后的android-emulator-runner配置:
- uses: reactivecircus/android-emulator-runner@v2
  with:
    api-level: ${{ matrix.api-level }}
    target: ${{ matrix.target }}
    disable-animations: true
    emulator-options: "-no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -memory 4096"
    script: ./gradlew connectedDebugAndroidTest

3. 确保Fragment生命周期正确推进

CI模拟器启动速度慢,Fragment可能未进入RESUMED状态就执行断言。优化测试初始化逻辑:

@RunWith(AndroidJUnit4::class)
class MapFragmentTest {
    private lateinit var scenario: FragmentScenario<MapFragment>

    @Before
    fun setup() {
        // 指定应用主题,避免默认主题与Fragment依赖不匹配
        scenario = launchFragmentInContainer(themeResId = R.style.Theme_YourApp)
        // 强制推进Fragment到RESUMED状态
        scenario.moveToState(Lifecycle.State.RESUMED)
    }

    @Test
    fun isLinearLayDisplayed() {
        onView(withId(R.id.search_bar_linearLayout)).check(matches(isDisplayed()))
    }

    // 其余测试方法复用已初始化的scenario,无需重复启动Fragment
}

4. 升级测试依赖版本

旧版本测试库存在CI环境兼容性问题,升级至稳定版:

androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
debugImplementation "androidx.fragment:fragment-testing:1.6.1"
androidTestImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'

5. 排查Fragment依赖的异步/全局状态

若MapFragment依赖第三方服务(如Google Maps)或异步初始化逻辑:

  • 确保CI模拟器的google_apis目标包含完整的Google服务
  • 为异步操作添加Espresso IdlingResource,或添加合理的等待逻辑(调试阶段可临时用Thread.sleep(1000),正式测试建议用IdlingResource)

内容的提问来源于stack exchange,提问作者Neyross

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 16:25:32