Robot Framework(15)- 擴展關鍵字
- 2020 年 5 月 30 日
- 筆記
- Robot Framework, 測試高級進階技能系列 - Robot Framework
如果你還想從頭學起Robot Framework,可以看看這個系列的文章哦!
//www.cnblogs.com/poloyy/category/1770899.html
前言
- 什麼是擴展關鍵字?就是你自己寫的 Python 文件,裡面包含了函數或者類
- 然後 RF 導入這個 Python 模組,就可以調用函數或者類方法,它們就是擴展關鍵字
Python 模組作為測試庫
模組文件名作為測試庫的名字
比如:Python 模組名叫 MyLibrary,文件名是 MyLibrary.py,那麼測試庫的名字就叫做 MyLibrary
Python 模組和 Robot 文件同目錄下的栗子
這是目錄結構哈
python 模組的程式碼
def returnlist(): return [i for i in range(10)] def return_dict(): return {"a": "hahhahahaahah"} # 以下劃線開頭的函數不能作為RF關鍵字 def _returnlist2(): return [1, 2]
robot 程式碼
進入test目錄下,運行以下命令
robot -P . test.robot
執行結果
知識點
- _前綴的方法不會作為關鍵字,在Python裡面, _ 開頭的方法是私有方法,RF 不會識別到它
- Python 方法作為關鍵字也是大小寫不敏感的
- RF 中會把關鍵字的 _ 和單個空格忽略掉,所以 returndict、return dict、return_dict 都是調用同一個關鍵字
Python 類作為測試庫的栗子
項目目錄
所有 Python 測試程式碼都在 tlib2.py 裡面哦
最終運行是在【15_擴展關鍵字】目錄下運行的,命令如下
robot -P . testrf
栗子一:類初始化不需要傳參
python 程式碼
class SubLibrary: def __init__(self): pass def returnint(self): return 2020 def _returnint2(self): return 4
robot 程式碼
測試結果
知識點
- 在類裡面, _ 前綴的方法不會當做關鍵字
- 同樣,類中聲明的方法當做關鍵字的話,大小寫不敏感
栗子二:類初始化需要傳參
python 程式碼
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))
robot 程式碼
測試結果
知識點
如果類的 __init__ 初始化方法需要傳參,則在導入庫後面跟對應的參數列表
拓展 Python 知識點:先有類對象,還是先執行類初始化方法?
__new__ 方法產生對象
__init__ 對象的初始化方法
先 new 一個對象,再 init 一個對象
栗子三:類名和模組名相同
python 程式碼
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))
robot 程式碼
測試結果
知識點
如果類名和模組名相同,可以不用導入類名
栗子四:使用路徑法導入 Python 模組
Python 程式碼用的還是栗子三的
robot 程式碼
測試結果
知識點
如果用路徑法,需要注意導入 Python 模組需要有文件後綴哦,且用 / 來表示目錄下
重點:使用路徑法,只能導入和模組名相同的類名!
Python 擴展庫的搜索規則
統一的規則
- 先根據 robot 文件自身當前目錄下查找庫文件
- 如果沒有找到則再根據 –pythonpath 和 -P 提供的搜索路徑進行搜索
- 最後找 Python 安裝的路徑
Python 庫引入了其他模組
背景
當 robot 文件導入的 Python 測試庫引入了其他模組時,應該怎麼寫導入路徑?
正確寫法
確保導入的模組路徑和RF導入的模組起始路徑統一
看栗子
testother.robot 導入 test.py 模組, test.py 模組引入了 login.py 模組的方法
目錄結構
login.py 程式碼
from robot.api import logger def login_test(): logger.console('test login')
test.py 程式碼
from pylib.login import login_test # from login import login_test 報錯 def test(): login_test()
robot 的程式碼
在 othertest 目錄下運行下面命令
robot -P . testother.robot
測試結果
結論
- 可以看到 robot 文件引入的路徑是 pylib 開頭, test 模組引入 login 模組的路徑也是 pylib 開頭
- 如果路徑是 login 開頭導入,那麼運行robot文件將會報錯(如下圖,包含了解析錯誤)
Python 庫中的 class 存在繼承
背景
當 robot 文件導入 Python 測試庫的類繼承了另一個類,應該怎麼寫導入路徑?
正確寫法
- 確保導入的模組路徑和RF導入的模組起始路徑統一
- 使用的時候 RF 文件只需導入子類即可
看栗子
test.robot 引入了 other.py 模組下的 Child 類,而 Child 類繼承了 Base.py 模組下的 Father 類
目錄結構
base.py 的程式碼
from robot.libraries.BuiltIn import logger class Father: def __init__(self): logger.console('init Father') def money(self): return '$10000'
other.py 的程式碼
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 的程式碼
在 testClass 目錄下運行下面命令
robot -P . test.robot
測試結果