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

查看:72
本文介绍了Android上的Espresso 2,在先前测试的活动仍然存在的情况下,无法启动被测活动后,中间测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Espresso 2来测试我的Android应用.中途,我看到测试随机失败,并显示以下Espresso失败消息:

I am using Espresso 2 for testing my Android app. Intermediately I see tests randomly fail with this Espresso failure message:

失败: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,已启用= 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} ...

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} ...

每次测试开始时,Espresso仪器都会打印出先前测试中仍然存在的活动数量:

Every beginning of a test the Espresso instrumentation prints out the number of activities that are still alive from a previous test:

MonitoringInstrumentation:仍处于创建为已停止"状态的活动:(数字)

每当我看到测试失败时,我总是会看到活动活动的数量不为0,这使我相信,这是导致此问题的原因,即使在大多数情况下,在进行测试之前就有活动活动仍然成功通过.

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.

发生这种情况时,设备只会在失败前显示主屏幕约10秒钟.当然,只有当我一次运行多个测试时,才会发生这种情况.

When this happens the device just shows the home screen for about 10 seconds before failing. This of course happens only when I run more than one test at once.

我的问题是,为什么测试之间会有实时活动,并且有一种方法可以让Instrumentation等到上一个测试的活动完成后再进行下一个测试.

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.

如果有人对测试中间失败的原因有不同的看法,这也将有所帮助.

If someone has a different idea to why the tests are failing intermediately that will also be helpful as well.

推荐答案

这个问题是在不久前提出的,但是由于这似乎仍然很重要,所以我想分享一下我发现的内容.我面临类似的问题-具有唯一ID的View上的AmbiguousViewMatcherException,不同的测试随机失败-可能是由于Activity没有被破坏.

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.

这似乎是一个已知问题,应该在下一个版本中解决(请参见此处: https://github.com/google/android-testing-support-library/issues/16 ).显然,每次测试后均无法可靠地调用onDestroy()和isFinishing(),因此您可能不会每次都获得一个新的Activity,甚至可能最终导致两个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). 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.

在他们修复该问题之前,我决定不浪费时间在解决方法上,而是开始使用Genymotion模拟器解决了我的问题-可能是因为它们的运行速度比Android Studio中的内置模拟器要快(建议此处).

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).

这篇关于Android上的Espresso 2,在先前测试的活动仍然存在的情况下,无法启动被测活动后,中间测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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