Espresso 2 on Android, intermediately tests fail a

2019-03-19 19:43发布

问题:

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

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

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

MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: (number)

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.

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.

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.

回答1:

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.

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.

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