MonkeyRunner 检测在测试执行期间发送 ShellCommandUnresponsiveException

MonkeyRunner instrumentation sends a ShellCommandUnresponsiveException during test execution

我正在尝试 运行 使用 MonkeyRunner 进行一些测试,但我无法以某种方式使其正常工作。经过仅几秒钟的测试,执行以 ShellCommandUnresponsiveException 停止,而测试实际上仍在设备上进行 运行ning。

我的 MonkeyRunner 脚本如下:

# -*- coding: utf-8 -*-

# Imports the monkeyrunner modules used by this program
import os
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Variables for commands
params = dict()
params['class'] = 'com.foo.test.TestCases#testMethod'

device.instrument('com.foo.test.test/android.test.InstrumentationTestRunner', params)

我得到了例外

160205 17:33:48.856:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] Error executing command: am instrument -w -r -e class com.foo.test.TestCases#testMethod com.foo.test.test/android.test.InstrumentationTestRunner
160205 17:33:48.856:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] com.android.ddmlib.ShellCommandUnresponsiveException
...

我不明白的是,如果我尝试将提到的命令与 adb shell 分开使用,它工作得很好并且测试通过,所以问题不是来自命令的语法或测试正在执行中。

我尝试用 device.shell 替换 device.instrument 并输入命令,虽然它不会抛出异常,但它不会等待测试结束后再继续其余部分代码。

问题可能来自于我的测试使用 Thread.sleep() 等待特定时间。我注意到其中一个睡眠时间非常短的测试似乎通过了并且没有导致异常。 有没有办法防止这种异常发生?我想我可以切换到 device.shell 方法并等待任意时间,但这会大大减慢我的测试速度,因为我无法预测测试将持续多长时间。

我认为您可以使用 AndroidViewClient/culebra to achieve your goal. More specifically if you take a look at RunTestsThread class 它几乎可以满足您的需求。 另外看看它的用法,主要是AdbClient的创建方式no timeout in UiAutomatorHelper.__runtests:

# We need a new AdbClient instance with timeout=None (means, no timeout) for the long running test service
newAdbClient = AdbClient(self.adbClient.serialno, self.adbClient.hostname, self.adbClient.port, timeout=None)
self.thread = RunTestsThread(adbClient=newAdbClient, testClass=self.TEST_CLASS, testRunner=self.TEST_RUNNER)
if DEBUG:
    print >> sys.stderr, "__runTests: starting thread"
self.thread.start()