前言
在写测试框架的一篇文章中提到过(python接口测试框架选择之pytest+yaml+Allure_亲爱的阿满啊的博客-CSDN博客_pytest yaml),yaml可以用来编写测试数据,yaml文件间的互相引用使测试数据的编写更灵活。比如接口之间会存在一些共性,存在上传的字段相同,这些相同的字段可以写在yaml文件里,需要上传这些字段的接口,它们的测试数据直接引用这个yaml文件。本文讲解使用pyyaml库,进行yaml文件间的引用。
一、yaml文件之间的相互引用
场景:所有接口都需要上传id,name,account三个字段
思路分析:将id,name,account三个字段写在一个文件里,其它接口引用这个文件。
代码编写
1.先安装pyyaml库,
pip install pyyaml-include
2.新建一个yaml文件,user.yaml
id: 12
name: "test"
account: "1234"
3.一个接口的请求数据的文件名为AllApi.yaml,在文件里引用user.yaml文件
ecpm: 100
type: 1
userInfo: !include user.yaml
4.查看文件的路径,后续需要传入路径
user.yaml:data/base/user.yaml
AllApi.yaml: data/aboutUser/AllApi.yaml
5.将user.yaml文件加入AllApi.yaml
basePath = "data/base"
rootfile = "data/aboutUser/AllApi.yaml"
YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader, base_dir=basePath)
with open(rootfile) as f:
data = yaml.load(f, Loader=yaml.FullLoader)
6.可以将上面的代码写成工具类,然后返回data
#rootfile:AllApi.yaml的文件路径data/aboutUser/AllApi.yaml
#basePath:user.yaml的根目录路径data/base
def add_base_yaml(rootfile,basePath):
YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader, base_dir=basePath)
with open(rootfile) as f:
data = yaml.load(f, Loader=yaml.FullLoader)
return data
打印结果
{'ecpm': 100, 'type': 1, 'userInfo': {'id': 12, 'name': 'test', 'account': '1234'}}
7.如果在data/base下还有其它文件,则都可以同时引用,假设info.yaml也在data/base这个目录下,在AllApi.yaml也可以引用。用第6点写的根据类就可以读取到数据。
ecpm: 100
type: 1
userInfo: !include userInfo.yaml
wxInfo: !include info.yaml
如何定basePath的路径
由第7点可以明白,引入的文件必须是basePath路径下的文件。假如AllApi.yaml需要同时引用两个文件,一个是userInfo.yaml,另一个文件是test.yaml,test文件路径为:data/aboutTest/TestApi.yaml,则add_base_yaml(rootfile,basePath)的basePath传入"data/"。yaml文件写为
ecpm: 100
type: 1
userInfo: !include base/userInfo.yaml
Info: !include aboutTest/info.yaml
打印结果
{'ecpm': 100, 'type': 1, 'userInfo': {'id': 12, 'name': 'test', 'account': '1234'}, 'Info': {'testInfo': 'test'}}
basePath是引入文件的共同根目录,可以按照整个项目的实际情况去定basePath。
二、yaml文件内部之间的相互引用
&符号标记锚点来进行引用
代码示例
name: test
sign1: &sign1 # &符号标记锚点1
user: "lili"
pw: "1234"
sign2: &sign2 # &符号标记锚点2
id: "1"
index: 5
test1:
<<: *sign1 # 方式一:使用 <<: * 符号引用
test2: *sign2 # 方式二:直接使用*号引用
打印
{'name': 'test', 'sign1': {'user': 'lili', 'pw': '1234'}, 'sign2': {'id': '1', 'index': 5}, 'test1': {'user': 'lili', 'pw': '1234'}, 'test2': {'id': '1', 'index': 5}}
三、yaml文件内部间相互引用
name: test
sign1: &sign1
user: !include base/baseUserInfo.yaml
pw: !include base/wxUserInfo.yaml
sign2: &sign2
id: "1"
index: 5
test1:
<<: *sign1
test2: *sign2
打印文章来源:https://www.toymoban.com/news/detail-400305.html
{'name': 'test', 'sign1': {'user': {'prdid': '2', 'phoneid': '123', 'eight_user_type': 0}, 'pw': {'appOpenid': 'a_Eef', 'unionId': 'oup', 'nickname': 'man'}}, 'sign2': {'id': '1', 'index': 5}, 'test1': {'user': {'prdid': '2', 'phoneid': '123', 'eight_user_type': 0}, 'pw': {'appOpenid': 'a_Eef', 'unionId': 'oup', 'nickname': 'man'}}, 'test2': {'id': '1', 'index': 5}}
以上就是yaml的常用用法,掌握以后,会在编写测试数据的yaml文件时更得心应手。文章来源地址https://www.toymoban.com/news/detail-400305.html
到了这里,关于接口测试框架pytest+yaml之yaml文件的相互引用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!