pytest + yaml 框架 -28.在调用函数的时候,传参可以引用变量了
前言
有小伙伴提到,接口返回一个id值,extract提取到后,后面根据这个id值拼接一个SQL语句,调用查询sql的函数。
这样调用函数的时候传参会嵌套引用另外一个变量。
此功能在v1.2.5 版本上实现
(备注:从v1.2.4 以后新版本不再公开,新功能内部 VIP 学员可以使用,公开版本仅解决bug, 不提供新功能了。)
使用示例
在conftest.py 定义任意函数, 传一个变量, 并注册到my_builtins
from pytest_yaml_yoyo import my_builtins
# 作者-上海悠悠 wx:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
def fun_x(x):
return f"result: {x}"
my_builtins.funo = fun_x
在yaml 用例文件中引用函数,标签传参引用其它变量
test_b1.yml
config:
name: xx
variables:
a: "hello"
b: "world"
test1:
name: 调用函数直接传变量
print: ${fun_x(a)}
test2:
name: 函数参数是字符串引用了变量
print: ${fun_x('select * from xx where name="${b}"')}
执行用例
pytest test_b1.yml
运行结果
test_b1.yml::test1
------------------------------- live log call -------------------------
2023-05-23 09:12:21 [INFO]: 执行文件-> test_b1.yml
2023-05-23 09:12:21 [INFO]: base_url->
2023-05-23 09:12:21 [INFO]: config variables-> {'a': 'hello', 'b': 'world'}
2023-05-23 09:12:21 [INFO]: 运行用例-> test1
2023-05-23 09:12:21 [INFO]: 取值表达式 fun_x(a)
2023-05-23 09:12:21 [INFO]: 取值结果:--result:hello----, <class 'str'>
2023-05-23 09:12:21 [INFO]: validate 校验内容-> []
2023-05-23 09:12:21 [INFO]: export 导出全局变量:{}
PASSED [ 50%]
test_b1.yml::test2
-------------------- live log call -------------------------------
2023-05-23 09:12:21 [INFO]: 执行文件-> test_b1.yml
2023-05-23 09:12:21 [INFO]: base_url->
2023-05-23 09:12:21 [INFO]: config variables-> {'a': 'hello', 'b': 'world'}
2023-05-23 09:12:21 [INFO]: 运行用例-> test2
2023-05-23 09:12:21 [INFO]: 取值表达式 fun_x('select * from xx where name="world"')
2023-05-23 09:12:21 [INFO]: 取值结果:--result:select * from xx where name="world"----, <class 'str'>
2023-05-23 09:12:21 [INFO]: validate 校验内容-> []
2023-05-23 09:12:21 [INFO]: export 导出全局变量:{}
PASSED [100%]
format 格式化方式
第二种解决方式,使用 jinja2 的 filter 过滤器语法,用到 format 格式化方法
# 作者-上海悠悠 wx:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
config:
name: xx
variables:
a: "hello"
b: "world"
test3:
name: format 格式化传字符串
print: ${'mame="%s"' | format("yoyo")}
test4:
name: format 格式化1个变量
print: ${'a="%s"' | format(a)}
test5:
name: format 格式化2个变量
print: ${'a="%s" b="%s"' | format(a, b)}
test6:
name: 函数中引用
print: ${fun_x('select * from xx where name="%s";' | format(a))}
运行结果
test_b1.yml::test3
---------------------------- live log call -----------------------
2023-05-23 09:32:54 [INFO]: 执行文件-> test_b1.yml
2023-05-23 09:32:54 [INFO]: base_url->
2023-05-23 09:32:54 [INFO]: config variables-> {'a': 'hello', 'b': 'world'}
2023-05-23 09:32:54 [INFO]: 运行用例-> test3
2023-05-23 09:32:54 [INFO]: 取值表达式 'mame="%s"' | format("yoyo")
2023-05-23 09:32:54 [INFO]: 取值结果: mame="yoyo", <class 'str'>
mame="yoyo"
2023-05-23 09:32:54 [INFO]: validate 校验内容-> []
2023-05-23 09:32:54 [INFO]: export 导出全局变量:{}
PASSED
test_b1.yml::test4
------------------------- live log call --------------------
2023-05-23 09:32:54 [INFO]: 执行文件-> test_b1.yml
2023-05-23 09:32:54 [INFO]: base_url->
2023-05-23 09:32:54 [INFO]: config variables-> {'a': 'hello', 'b': 'world'}
2023-05-23 09:32:54 [INFO]: 运行用例-> test4
2023-05-23 09:32:54 [INFO]: 取值表达式 'a="%s"' | format(a)
2023-05-23 09:32:54 [INFO]: 取值结果: a="hello", <class 'str'>
a="hello"
2023-05-23 09:32:54 [INFO]: validate 校验内容-> []
2023-05-23 09:32:54 [INFO]: export 导出全局变量:{}
PASSED
test_b1.yml::test5
------------------------ live log call --------------------------
2023-05-23 09:32:54 [INFO]: 执行文件-> test_b1.yml
2023-05-23 09:32:54 [INFO]: base_url->
2023-05-23 09:32:54 [INFO]: config variables-> {'a': 'hello', 'b': 'world'}
2023-05-23 09:32:54 [INFO]: 运行用例-> test5
2023-05-23 09:32:54 [INFO]: 取值表达式 'a="%s" b="%s"' | format(a, b)
2023-05-23 09:32:54 [INFO]: 取值结果: a="hello" b="world", <class 'str'>
a="hello" b="world"
2023-05-23 09:32:54 [INFO]: validate 校验内容-> []
2023-05-23 09:32:54 [INFO]: export 导出全局变量:{}
PASSED
test_b1.yml::test6
--------------------------- live log call ------------------------
2023-05-23 09:32:54 [INFO]: 执行文件-> test_b1.yml
2023-05-23 09:32:54 [INFO]: base_url->
2023-05-23 09:32:54 [INFO]: config variables-> {'a': 'hello', 'b': 'world'}
2023-05-23 09:32:54 [INFO]: 运行用例-> test6
2023-05-23 09:32:54 [INFO]: 取值表达式 fun_x('select * from xx where name="%s";' | format(a))
2023-05-23 09:32:54 [INFO]: 取值结果: --result:select * from xx where name="hello";----, <class 'str'>
--result:select * from xx where name="hello";----
2023-05-23 09:32:54 [INFO]: validate 校验内容-> []
2023-05-23 09:32:54 [INFO]: export 导出全局变量:{}
PASSED
网易云完整视频课程https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)