慢下来浓缩咖啡

时间:2016-03-23 00:54:22

标签: android automated-tests integration-testing instrumentation android-espresso

这是关于Espresso的。我在模拟器上成功运行集成测试。我认为有些测试失败了,因为它运行得太快了。有没有办法减慢执行/播放速度?

4 个答案:

答案 0 :(得分:5)

测试失败导致速度变得不可能。 Espresso可以将所有测试操作与被测应用程序同步。默认情况下,Espresso会等待当前消息队列中的UI事件进行处理,并在进入下一个测试操作之前完成默认AsyncTasks。但是,如果这对您的应用程序来说还不够,您可以告诉Espresso什么时候闲置,什么时候不闲置。为此,你必须:

  • 实施IdlingResource 接口
  • 在测试设置中调用Espresso.registerIdlingResource,向Espresso注册一个或多个IdlingResource。

如果您需要更多帮助,请问我!!

答案 1 :(得分:0)

哈哈......实际上Espresso只是这样。您遇到的问题是UI事件无法完成(例如,在列表从网络调用加载之前单击列表项)。 在这种情况下,从其他线程加载资源的地方,您可以实际执行Thread.sleep(millis)或更高效的UiController的loopMainThreadForAtleast(millis)方法等待something to load(事件完成)

答案 2 :(得分:0)

当您在Android Studio中录制Espresso测试时,当有视图交互来处理延迟时,它会自动将Sleep语句添加到测试中。这是与评论一起生成的方法:

// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html

try {
    Thread.sleep(700);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Link to the docs

答案 3 :(得分:-1)

我也有这个问题。我通过从Developer选项中删除设备上的活动动画来解决。

如果问题仍然存在,您可以在测试中使用sleep来减慢速度。

SystemClock.sleep(1000);

enter image description here