如果你还想从头学起Robot Framework,可以看看这个系列的文章哦!
https://www.cnblogs.com/poloyy/category/1770899.html
模块文件名作为测试库的名字
比如:Python 模块名叫 MyLibrary,文件名是 MyLibrary.py,那么测试库的名字就叫做 MyLibrary
这是目录结构哈
def returnlist():
return [i for i in range(10)]
def return_dict():
return {"a": "hahhahahaahah"}
# 以下划线开头的函数不能作为RF关键字
def _returnlist2():
return [1, 2]
robot -P . test.robot
_
_
所有 Python 测试代码都在 tlib2.py 里面哦
最终运行是在【15_扩展关键字】目录下运行的,命令如下
robot -P . testrf
class SubLibrary:
def __init__(self):
pass
def returnint(self):
return 2020
def _returnint2(self):
return 4
_
from robot.api import logger
class SubLibrary2:
def __init__(self, host, port, table='test'):
self.host = host
self.port = port
self.table = table
def printaddr2(self):
logger.console('host:%s,port:%s,table:%s' % (self.host, self.port, self.table))
如果类的 __init__ 初始化方法需要传参,则在导入库后面跟对应的参数列表
__new__ 方法产生对象
__init__ 对象的初始化方法
先 new 一个对象,再 init 一个对象
from robot.api import logger
class tlib2:
def __init__(self, host, port):
self.host = host
self.port = port
def printaddr(self):
logger.console('host:%s,port:%s' % (self.host, self.port))
如果类名和模块名相同,可以不用导入类名
Python 代码用的还是栗子三的
如果用路径法,需要注意导入 Python 模块需要有文件后缀哦,且用 / 来表示目录下
重点:使用路径法,只能导入和模块名相同的类名!
--pythonpath
当 robot 文件导入的 Python 测试库引入了其他模块时,应该怎么写导入路径?
确保导入的模块路径和RF导入的模块起始路径统一
testother.robot 导入 test.py 模块, test.py 模块引入了 login.py 模块的方法
from robot.api import logger
def login_test():
logger.console('test login')
from pylib.login import login_test
# from login import login_test 报错
def test():
login_test()
robot -P . testother.robot
robot
login
当 robot 文件导入 Python 测试库的类继承了另一个类,应该怎么写导入路径?
test.robot 引入了 other.py 模块下的 Child 类,而 Child 类继承了 Base.py 模块下的 Father 类
from robot.libraries.BuiltIn import logger
class Father:
def __init__(self):
logger.console('init Father')
def money(self):
return '$10000'
from robot.api import logger
from pylib.Base import Father
class Child(Father):
def __init__(self):
Father.__init__(self)
logger.console('init Child')
def use_money(self):
return self.money()
def make_money(self):
return '$9999'
robot -P . test.robot