Search code examples
androidmonkeyrunner

How to use MonkeyDevice.instrument?


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.


Solution

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