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

标签 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

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

最佳答案

机器人没有任何内置功能可供您执行。从机器人的角度来看,“免费”只是一根绳子。您需要将其转换为实际的函数对象。我可以想出几种不同的方法来做到这一点。

如果 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)

关于python - Robot Framework - 将库函数作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151038/

相关文章:

python - numpy 数组中看似不一致的切片行为

python - 在 Pandas 中按组连续获得最长的连续数周

python - Pandas,根据多行中的其他列创建新列

robotframework - 如何查看机器人脚本运行日志?

python-3.x - 忽略或解决机器人框架中测试自动化的证书警告

robotframework - 如何检查 Robot Framework 中的变量类型?

python - 在 'while True' 循环中线程化

python - 如何在不使用列表的情况下转换字符串中的数字?

python - 如何访问yaml文件中另一个变量内的变量?

xpath - 如何获取Span Table第n列匹配文本的元素数