接口测试框架pytest+yaml之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。

&符号标记锚点来进行引用
代码示例

name: test
sign1: &sign1 # &符号标记锚点1
  user: "lili"
  pw: "1234"
sign2: &sign2 # &符号标记锚点2
  id: "1"
  index: 5
test1:
  <<: *sign1 # 方式一:使用 <<: * 符号引用 test2: *sign2 方式二:直接使用*号引用 < code></:>
{'name': 'test', 'sign1': {'user': 'lili', 'pw': '1234'}, 'sign2': {'id': '1', 'index': 5}, 'test1': {'user': 'lili', 'pw': '1234'}, 'test2': {'id': '1', 'index': 5}}
name: test
sign1: &sign1
  user: !include base/baseUserInfo.yaml
  pw: !include base/wxUserInfo.yaml
sign2: &sign2
  id: "1"
  index: 5
test1:
  <<: *sign1 test2: *sign2 < code></:>
{'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文件时更得心应手。

Original: https://blog.csdn.net/weixin_36363079/article/details/123968334
Author: 亲爱的阿满啊
Title: 接口测试框架pytest+yaml之yaml文件的相互引用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772195/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球