Robot Framework - 将库函数作为参数传递

时间:2015-03-19 17:19:10

标签: python robotframework

我得到了一个简单的测试用例,我无法在我的testLib.py中工作。我有:

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

然后在Robot Framework测试文件mytest.robot中我有:

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

*** Test Cases ***

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

当我运行框架时,我得到:

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

知道如何将成员和自由函数传递给库吗?

1 个答案:

答案 0 :(得分:2)

你可以做任何没有内置于机器人的东西。从机器人的角度来看,“免费”只是一个字符串。您需要将其转换为实际的功能对象。我可以想到几种不同的方法来做到这一点。

如果free是关键字,您可以像这样定义free_run

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

另一种选择是在globals()返回的结果中查找函数名称,如果可以安全地假设func引用全局函数:

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