Android 测试:断言 activity 转换为另一个

Android testing: asserting an activity transitioned to another

我正在查看 tests on Android activities. I would like to test the activity lifecycle and particularly the end of my activity to start another activity (I am testing the end of the first activity, not the start of the second). I am using ActivityInstrumentationTestCase2 和 Android Espresso with JUnit 4 测试。

我试图断言,一旦我通过按下屏幕上的按钮触发 activity 的结束,activity 就会暂停或不可见,但我 运行到目前为止运气不好。是否有任何好的做法来测试活动之间的 t运行sitions?

你应该看看 https://developer.android.com/training/activity-testing/activity-functional-testing.html#activitymonitor

基本上你需要添加并注册一个 ActivityMonitor in your instrumented functional test, which will be set up to listen for your second Activity. Once the trigger happens (ie a button being clicked in your test case) you will just use waitForActivityWithTimeout(int) 来获取第二个 Activity 的实例。

如果调用超时,或者结果为空,那么您就知道您的第二个 Activity 尚未开始。您还可以在 Activity 实例上断言它们是否 finishing/destroyed/etc 取决于您期望它们发生什么。