简体   繁体   English

Espresso未启动活动在参数化测试中进行第二次迭代

[英]Espresso not starting Activity the same for second iteration in parameterised test

Is there something different about how Espresso runs tests after the first one when using parameterised tests? 在第一次使用参数化测试后,Espresso如何运行测试会有什么不同吗?

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). 所以可能有一个错误,但我倾向于认为这是我正在做的事情(或不做)。

The actual problem that occurs is that a field gets set to null halfway through the test (according to the debugger). 发生的实际问题是在测试中途将字段设置为null(根据调试器)。 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. 所以我在代码中找不到任何明显的东西,但我也看不出与Espresso有什么不同。 Any ideas? 有任何想法吗?

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. 我跟着调试器,它向我证明了值已设置,然后由于某种原因,当它暂停当前Activity以启动对话框时,值被设置为null。 But only on the second test. 但仅限于第二次测试。 In this exact same situation on the first test it doesn't do that. 在第一次测试的完全相同的情况下,它没有这样做。 Why? 为什么?

Edit: 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. 这有点奇怪,但我通过调试器发现,第一次测试中的onDetach在onResume 之后被调用进行第二次测试。 Why might that be? 为什么会这样?

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. 无论如何,这肯定是我遇到问题的原因,因为使值为null的代码来自onDetach,并且该值最初是从onResume中的方法设置的。 So that explains it. 所以这解释了它。 But why is this happening? 但为什么会这样呢?


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

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. 由于在当前测试中在onResume之后调用了之前测试的onDestroy,因此我将onDestroy中的代码移动到清除我的监听器到onResume的开头。 Now when the other code in onResume runs it's not nullified by the bad timing of onDestroy. 现在,当onResume中的其他代码运行时,它不会因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. 所有测试都通过,手动运行时实际应用程序也能正常运行。

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. 原因是当我为结果调用另一个Activity时,当它返回时,监听器在onResume中被杀死。 In this case onDestroy isn't killed yet so would not cause this problem. 在这种情况下,onDestroy尚未被杀死,因此不会导致此问题。

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. 编辑2和当前的解决方案 :这个问题对于读者来说有点难以测试,所以我创建了一个测试应用程序和一个更简单的新问题。 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 我已经找到了一个工作解决方案,我在这篇文章中已经解释过: Espresso没有等到活动被破坏,在为下一次测试创建新的活动之前

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. 我相信基于阅读一些Meta Q&A,我们更喜欢这些问题没有删除,所以我只想在这里留下这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM