Saturday, April 23, 2011

Running instrumentation from monkeyrunner

Sometimes you may want to run the instrumentation for a package from monkeyrunner, and the following script does just this. It has the advantage of using the package manager to find the correct instrumentation for the specified package simplifying the parameters you must provide.

#! /usr/bin/env monkeyrunner

import sys
import os
from com.android.monkeyrunner import MonkeyRunner

PLI = 'pm list instrumentation'
prog = os.path.basename(sys.argv[0])

def usage():
   print >>sys.stderr, \
       "usage: %s target-package-name" % prog
   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()

You can invoke this script as in the following command line:

   $ instrumentation.mr com.example.package

compare this against the command line you may need to invoke the same instrumentation using plain am instrument.
Once you invoke the script with the correct package name, the instrumentation for that package is run and the results are presented, like in this example:


waiting for connection...
running istrumentation for com.example.package


Test results for InstrumentationTestRunner=..................................
Time: 37.844


OK (34 tests)

Post a Comment