接口自动化测试中,参数化时常用到yaml文件,语法参考:https://www.runoob.com/w3cnote/yaml-intro.html
json和yaml文件互转,也可使用:https://oktools.net/json2yaml
手写容易写错,python中yaml.safe_dump()方法可自动生成yaml文件
import yaml
def test_yaml():
"""
自动生成yaml文件
"""
apollo = {
"default": "test",
"www.lingtiku.com": {
"dev": "127.0.0.1",
"test": "127.0.0.2",
"prod": "127.0.0.3"
}
}
with open("apollo.yaml","w") as f:#写文件
yaml.safe_dump(data=apollo,stream=f)
执行代码,pycharm目录中中会自动生成apollo.yaml文件
default: test
www.lingtiku.com:
dev: 127.0.0.1
test: 127.0.0.2
prod: 127.0.0.3
yaml.safe_load()读上面生成的apollo.yaml文件
apollo = yaml.safe_load(open("apollo.yaml"))