user57754
user57754

Reputation: 55

Robot Framework - pass library function as parameter

I got a simple test case which I can't get to work in my testLib.py. I have:

def free(arg):
  print "found arg \"{0}\"".format(arg)


class testLib:

  def free_run(self,func,arg):
    print "this is free test"
    func(arg)

  def member_func(self,arg):
    print "mem func arg={0}".format(arg)

if __name__ == "__main__":
  x = testLib();
  x.free_run(free,"hello world")
  x.free_run(x.member_func,"free - mem test")

Then in Robot Framework test file mytest.robot I have:

*** Setting ***
Library         MainLib.py  
Library         testLib.py

*** Test Cases ***

test2
  free run       free           "testing free run"
  self run       member_func    "testing self run"

When I run the framework, I get:

==============================================================================
test2                                                                 | FAIL |
TypeError: 'unicode' object is not callable

Any idea how to pass the member and free function to the library?

Upvotes: 2

Views: 4084

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

There's nothing built-in to robot that you can do. From robot's perspective, "free" is just a string. You need to convert that to an actual function object. I can think of a couple different ways to do this.

If free were a keyword, you could define free_run like this:

from robot.libraries.BuiltIn import BuiltIn
def free_run(self,func,arg):
  print "this is free test"
  BuiltIn().run_keyword(func, arg)

Another option would be to look up the function name in the result returned from globals(), if it's safe to assume that func refers to a global function:

def free_run(self,func_name,arg):
    print "this is free test"
    func = globals()[func_name]
    func(arg)

Upvotes: 2

Related Questions