问题
HI guys, I'm trying to run one of my test instrumentation from my MonkeyRunner script. Unfortunately I can't get it to work. I've tried calling MonkeyDevice.instrument with different variations of the parameters but had no luck.
I've tried
device = MonkeyRunner.waitForConnection() device.instrument("android.test.InstrumentationTestRunner") device.instrument("com.myTestPackage.myTestClass") device.instrument("com.myTestPackage/.myTestClass") device.instrument("myTestClass")
None of these throw and error but they don't run the test either. I can run my instrumentation via Dev Tools or though Android Junit Test so I know it works.
So can someone tell me the correct to use this method? Thanks.
回答1:
You are probably using wrong arguments. This script, which I named instrumentation.mr
, helps you to use the right ones.
Invoke it using you target package name.
#! /usr/bin/env monkeyrunner
import sys
from com.android.monkeyrunner import MonkeyRunner
PLI = 'pm list instrumentation'
def usage():
print >>sys.stderr, "usage: intrumentation.mr target-package"
sys.exit(1)
def main():
if len(sys.argv) != 2:
usage()
pkg = sys.argv[1]
print "waiting for connection..."
device = MonkeyRunner.waitForConnection()
print "running istrumentation for %s" % pkg
for (i, t) in map(lambda l: l.split(), device.shell(PLI).splitlines()):
if t == '(target=%s)' % pkg:
print device.instrument(i.split(':')[1], { 'wait':True })['stream']
return
print >>sys.stderr, "ERROR: instrumentation for %s not found" % pkg
if __name__ == '__main__':
main()
For example:
$ instrumentation.mr com.example.aatg.tc
prints:
waiting for connection...
running istrumentation for com.example.aatg.tc
Test results for InstrumentationTestRunner=...............................
Time: 39.932
OK (31 tests)
回答2:
The MonkeyDevice.instrument(string class, dictionary args) maps onto the InstrumentationTestRunner commands. The Android documentation has some good info on the instrumentation command, here.
What the documentation does NOT tell you is how to specify args. I found that in the android source code. See AdbChipDevice line 483. Here's a copy and paste of the code:
482 @Override
483 public Map<String, Object> instrument(String packageName, Map<String, Object> args) {
484 List<String> shellCmd = Lists.newArrayList("am", "instrument", "-w", "-r");
485 for (Entry<String, Object> entry: args.entrySet()) {
486 final String key = entry.getKey();
487 final Object value = entry.getValue();
488 if (key != null && value != null) {
489 shellCmd.add("-e");
490 shellCmd.add(key);
491 shellCmd.add(value.toString());
492 }
493 }
494 shellCmd.add(packageName);
495 String result = shell(shellCmd.toArray(ZERO_LENGTH_STRING_ARRAY));
496 return convertInstrumentResult(result);
497 }
So the following python monkey code:
params = dict()
params['size'] = 'small'
device = MonkeyRunner.waitForConnection()
device.instrument('com.mycompany.myapp/android.test.InstrumentationTestRunner', params)
Is equivalent to the following command:
adb shell am instrument -w -r -e size small com.mycompany.myapp/android.test.InstrumentationTestRunner
来源:https://stackoverflow.com/questions/5690173/how-to-use-monkeydevice-instrument