在参数化测试中,Espresso无法在第二次迭代中启动Activity相同 [英] Espresso not starting Activity the same for second iteration in parameterised test

查看:76
本文介绍了在参数化测试中,Espresso无法在第二次迭代中启动Activity相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用参数化测试时,Espresso在第一个测试之后运行测试的方式是否有所不同?

Is there something different about how Espresso runs tests after the first one when using parameterised tests?

我有两个参数,如果我自己做的话(如果我注释掉其中一个并只有一个参数),或者如果我颠倒了第一个参数始终通过的顺序,它们都可以很好地运行.

I have two parameters and they both run just fine if I do them on their own (if I comment one of them out and just have one parameter), or if I reverse the order the first one always passes.

但是由于某些原因,当我一起运行它们时,第二个总是失败.实际上,有一次它没有失败.在不做任何更改的情况下,它可以工作一次.但是只有一次.因此,可能有一个错误,但我倾向于认为这是我在做(或不做)的事情.

But for some reason when I run them together the second one always fails. Actually, one time it didn't fail. Without changing anything it worked one time. But just once. So it's possible maybe there's a bug but I'm inclined to think it's something I'm doing (or not doing).

发生的实际问题是,在测试的一半(根据调试器),字段被设置为null.但是我似乎无法弄清楚如何或为什么,特别是因为它在第一次测试中没有发生.因此,我在执行此操作的代码中找不到任何明显的内容,但是我也看不到Espresso的不同之处.有什么想法吗?

The actual problem that occurs is that a field gets set to null halfway through the test (according to the debugger). But I can't seem to figure out how or why, especially since it doesn't happen on the first test. So I can't find anything obvious in the code that's doing it, but I also can't see what's different with Espresso either. Any ideas?

我一直跟随调试器,在调试器中向我证明了该值已设置,然后由于某种原因,当它暂停当前的Activity以启动对话框时,该值将设置为null.但是只在第二次测试.在第一次测试的完全相同的情况下,它不会这样做.为什么?

I've followed the debugger where it proved to me that the value is set, and then for some reason when it pauses the current Activity to launch a dialog the value gets set to null. But only on the second test. In this exact same situation on the first test it doesn't do that. Why?

修改: 好的,我进一步缩小了范围,但仍然不确定为什么要这样做.有点奇怪,但是我已经通过调试器发现,在第二个测试中,第一个测试中的onDetach在onResume之后被称为 .为什么会这样?

Ok I've narrowed it down further but still not sure why it's doing this. It's a bit strange but I've discovered through the debugger, that onDetach in the first test is being called after onResume for the second test. Why might that be?

无论如何,这绝对是我遇到问题的原因,因为使值设为null的代码源自onDetach,而该值最初是由onResume中的方法设置的.这样就可以解释了.但是为什么会这样呢?

Anyway that's definitely why I'm having a problem as the code that's making the value null originates from onDetach, and the value is originally set from a method in onResume. So that explains it. But why is this happening?

问题在此处登录: https://code.google.com/p/android/issues/detail?id = 235247

推荐答案

好,我找到了解决方案.由于在当前测试中在onResume之后调用了先前测试的onDestroy,因此我将代码移至onDestroy中,以将侦听器清除到onResume的开头.现在,当onResume中的其他代码运行时,不会因onDestroy的错误计时而无效.通过存储库访问侦听器,该存储库始终保持活动状态.

Ok I found a solution. Since onDestroy from the previous test is being called after onResume in the current test, I moved the code in onDestroy that clears my listeners to the start of onResume. Now when the other code in onResume runs it's not nullified by the bad timing of onDestroy. The listeners are accessed via the repository which stays active the whole time.

希望这对任何阅读有道理的人都是有意义的.到目前为止,我还没有注意到此替代方法的任何副作用.手动运行时,所有测试均通过,实际应用程序也能正常运行.

Hopefully this all makes sense to anyone reading in case they face a similar problem. So far I haven't noticed any side effects of this workaround. All the tests pass and the actual app works as well when running manually.

编辑:此解决方法现在引起了问题,因此我不得不接受我的回答.原因是因为当我调用另一个Activity作为结果时,当它返回时,侦听器在onResume中被杀死.在这种情况下,onDestroy尚未被杀死,因此不会导致此问题.

Edit: This workaround is now causing problems so I have to unaccept my answer. The reason is because when I call another Activity for result, when it comes back the listeners are killed in onResume. In this case onDestroy isn't killed yet so would not cause this problem.

编辑2和当前解决方案:对于读者来说,这个问题有点难以测试,因此我创建了一个测试应用程序和一个新的更简单的问题.此后,我找到了一个有效的解决方案,已在本文中进行了解释:

Edit 2 and current solution: this question was a bit hard to test for readers so I created a test app and a new simpler question. I've since found a working solution that I've explained in this post: Espresso not waiting till Activity is destroyed, before creating a new one for the next test

我认为,根据阅读的一些Meta Q& A信息,SO希望不要删除问题,因此我将在此保留该问题.

I believe based on reading some Meta Q&A's that SO prefers that questions aren't deleted so I'll just leave this question here.

这篇关于在参数化测试中,Espresso无法在第二次迭代中启动Activity相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆