2016-02-05 16 views
1

我試圖運行一些使用MonkeyRunner的測試工具,但我無法讓它像應該那樣工作。經過幾秒鐘的測試後,執行過程停止,執行結果爲ShellCommandUnresponsiveException,而測試實際上仍在設備上運行。MonkeyRunner工具在測試執行過程中發送ShellCommandUnresponsiveException

我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方法並等待任意時間,但這會顯着減慢我的測試,因爲我無法預測測試的時間。

回答

0

我認爲你可以用AndroidViewClient/culebra來實現你的目標。更具體地說,如果你看看RunTestsThread class它幾乎正是你所需要的。 也看看它的使用,主要AdbClient沒有超時創建的方式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() 
+0

了一段時間,使這項工作,因爲我與Python初學者,但它運行良好。創建沒有超時的adb客戶端似乎是解決我的問題的方法。謝謝 ! – Hyruu

相關問題