前言
經常看到很多同學問到,如何在 yaml 文件中引用一個 python 的函數?
問題分析
大家對yaml文件還處於比較陌生的階段,yaml 和 json 文件本質上是一樣的,都是靜態的文件,當然不能直接引用 python 的函數。
那這時候就有人問到了,那為什么 httprunner 框架可以在yaml文件中引用函數呢?
- 這是因為 httprunner 框架封裝過對 yaml 文件的讀取了,它是先讀取文件內容,正則提取到
${}
括號里面的函數內容,再把函數的值替換過去
那么我們能不能實現這種效果呢?
- 當然是可以的,可以參考httprunner的實現,也可以用到 python 的模板 jinja2 來實現。
使用模板可以編寫出可讀性更好,更容易理解和維護的代碼,並且使用范圍非常廣泛,因此怎么使用模板主要取決於我們的想象力和創造力。
python的模板庫jinja2 功能是非常強大的。
jinja2 模板庫
先需要pip安裝
pip install jinja2
render 函數實現
在yaml文件中,通過 {{ 函數名稱() }}
來引用函數
寫個 render 函數讀取 yaml 文件內容
import os
import jinja2
import yaml
import random
def render(tpl_path, **kwargs):
path, filename = os.path.split(tpl_path)
return jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')
).get_template(filename).render(**kwargs)
讀取到的yaml文件本質上都是字符串來讀取的,通過jinja2 模板來讀取,會先把函數的值替換進去。最后再轉成python的dict結構
import os
import jinja2
import yaml
import random
"""
作者:上海-悠悠
python QQ交流群:730246532
聯系微信/QQ: 283340479
"""
def render(tpl_path, **kwargs):
path, filename = os.path.split(tpl_path)
return jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')
).get_template(filename).render(**kwargs)
# yaml 文件調用以下函數
def rand_str():
return str(random.randint(1000000, 2000000))
if __name__ == '__main__':
r = render("aa.yml", **{"rand_str": rand_str})
print(r)
print(yaml.safe_load(r))
運行結果
name: yoyo
age: 22
tel: 1616350
{'name': 'yoyo', 'age': 22, 'rand_str': 1616350}
上面讀取函數是寫死的,我們希望能自動加載類似於debugtalk.py的文件來自動加載函數
自動加載debug.py里面的函數
寫一個debug.py 文件,實現 yaml 文件里面定義的函數去替換值。
import random
"""
作者:上海-悠悠
python QQ交流群:730246532
聯系微信/QQ: 283340479
"""
# yaml 文件調用以下函數
def rand_str():
return str(random.randint(1000000, 2000000))
run.py里面定義一個函數自動讀取debug.py里面的函數,生成dict 鍵值對格式
def all_functions():
"""加載debug.py模塊"""
debug_module = importlib.import_module("debug")
all_function = inspect.getmembers(debug_module, inspect.isfunction)
# print(dict(all_function))
return dict(all_function)
函數返回 {'rand_str': <function rand_str at 0x0000017B72EA8488>}
完整的run.py文件內容
import os
import jinja2
import yaml
import importlib
import inspect
"""
作者:上海-悠悠
python QQ交流群:730246532
聯系微信/QQ: 283340479
"""
def render(tpl_path, **kwargs):
"""渲染yml文件"""
path, filename = os.path.split(tpl_path)
return jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')
).get_template(filename).render(**kwargs)
def all_functions():
"""加載debug.py模塊"""
debug_module = importlib.import_module("debug")
all_function = inspect.getmembers(debug_module, inspect.isfunction)
print(dict(all_function))
return dict(all_function)
if __name__ == '__main__':
r = render("aa.yml", **all_functions())
print(r)
print(yaml.safe_load(r))
運行結果
{'rand_str': <function rand_str at 0x000001931C248488>}
name: yoyo
age: 22
tel: 1010421
{'name': 'yoyo', 'age': 22, 'tel': 1010421}
網易雲完整視頻課程《pytest+yaml 框架使用與開發》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
報名咨詢wx:283340479 (已報名的同學學習過程中有問題,都可以協助解決)