简体   繁体   English

Android上的Espresso 2,在先前测试的活动仍然存在的情况下,无法启动被测活动后,中间测试失败

[英]Espresso 2 on Android, intermediately tests fail after failing to start the activity under test while activities from previous tests are still alive

I am using Espresso 2 for testing my Android app. 我正在使用Espresso 2来测试我的Android应用。 Intermediately I see tests randomly fail with this Espresso failure message: 中间,我看到此Espresso失败消息随机导致测试失败:

failed: testLongPressXXXXX (com.company.xxx.tests.testSuite.Test) ----- begin exception ----- android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.company.xxx:id/xxx_view_id View Hierarchy: +>DecorView{id=-1, visibility=VISIBLE, width=729, height=319, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} ... 失败:testLongPressXXXXX(com.company.xxx.tests.testSuite.Test)-----开始异常----- android.support.test.espresso.NoMatchingViewException:找不到层次结构中符合ID为com的视图。 company.xxx:id/xxx_view_id视图层次结构:+> DecorView {id = -1,可见性=可见,宽度= 729,高度= 319,has-focus = false,has-focusable = false,has-window-focus = true ,is-clickable = false,is-enabled = true,is-focused = false,is-focusable = false,is-layout-requested = false,is-selected = false,root-is-layout-requested = false,具有-input-connection = false,x = 0.0,y = 0.0,child-count = 1} ...

Every beginning of a test the Espresso instrumentation prints out the number of activities that are still alive from a previous test: 在每次测试开始时,Espresso仪器都会打印出先前测试中仍然存在的活动数量:

MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: (number) MonitoringInstrumentation:仍处于“创建为已停止”状态的活动:(数量)

Whenever I see a failed test I always see that the number of alive activities is not 0 which led me to believe that this is the reason for this issue even though most of the time when there are live activities before a test the test still passes successfully. 每当我看到测试失败时,我总是会看到活动活动的数量不为0,这使我相信,这是导致此问题的原因,即使在大多数情况下,在进行测试之前仍然存在活动活动,测试仍然可以成功通过。

When this happens the device just shows the home screen for about 10 seconds before failing. 发生这种情况时,设备会在故障之前仅显示主屏幕约10秒钟。 This of course happens only when I run more than one test at once. 当然,只有当我一次运行多个测试时,才会发生这种情况。

My question is why are there live activities between tests and is there a way to make the Instrumentation wait until the activities from the previous test are finished before proceeding to the next test. 我的问题是,为什么测试之间存在实时活动,并且有一种方法可以使Instrumentation等待直到上一个测试的活动完成后再进行下一个测试。

If someone has a different idea to why the tests are failing intermediately that will also be helpful as well. 如果有人对测试中间失败的原因有不同的看法,那也将有所帮助。

This question was asked a while ago but since this still seems to be relevant I thought I'd share what I found. 不久前有人问了这个问题,但是由于这似乎仍然很重要,我想我会分享我发现的东西。 I am facing a similar problem - AmbiguousViewMatcherException on Views that have a unique id, different tests failing randomly - probably due to Activities not being destroyed. 我面临类似的问题-具有唯一ID的View上的AmbiguousViewMatcherException,不同的测试随机失败-可能是由于Activity没有被破坏。

This seems to be a known issue and should be addressed in the next release (see here: https://github.com/google/android-testing-support-library/issues/16 ). 这似乎是一个已知问题,应该在下一个版本中解决(请参阅此处: https : //github.com/google/android-testing-support-library/issues/16 )。 Apparently onDestroy() and isFinishing() are not called reliably after each test so you might not get a fresh Activity every time or even end up with two Activities on top of each other. 显然,每次测试后均无法可靠地调用onDestroy()和isFinishing(),因此您可能不会每次都获得一个新的Activity,甚至可能最终使两个Activity彼此重叠。

Until they fix it I've decided not to waste my time on a workaround but starting using Genymotion emulators which solved my problem - possibly because they run faster than the built-in ones in Android Studio (as was suggested here ). 直到他们解决它,我决定不浪费我的一个解决方法的时间,但使用Genymotion模拟器这解决了我的问题开始-可能是因为它们的运行速度比内置的那些Android Studio中(如建议加快在这里 )。

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

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