https://blog.csdn.net/weixin_45912307/article/details/109609628
yaml文件封装
# —— encoding :utf-8 ——
# @time: 2020/11/10 20:37
# @IDE: apiTest_frameworkv1.0
# @Author: xxxx
# @Email: xxxxxx@qq.com
# @File: dl_yaml.py
import datetime
import os
from sys import path
import yaml
# pip install pyyaml
class DLYaml:
env = {
"default": "test",
"testing-studio":
{
"dev":"127.0.0.1",
"test":"127.0.0.2"
}
}
def dump_yaml(self):
# 生成现在的时间
# t = datetime.datetime.now()
# 对现在时间格式化,以此作为文件名
# file_name = os.path.join(path+t.strftime('%Y%m%d %H:%M:%S')+'.yaml')
# os.mkdir(file_name)
file_name = 'env.yaml'
with open(file_name,'w') as f:
yaml.safe_dump(data=self.env,stream=f)
def load_yaml(self,file_name):
res = yaml.safe_load(open(file_name))
return res
if __name__ == '__main__':
DLYaml().dump_yaml()
# data = DLYaml().load_yaml()
# print(data)
调用yaml函数读取文件
import logging
from multiApiTest import dl_yaml
class TestYaml():
def test_dump_yaml(self):
try:
logging.info('正在写入yaml文件')
dl_yaml.DLYaml().dump_yaml()
except Exception as e:
logging.exception('yaml文件写入错误:',e)
def test_load_yaml(self):
try:
logging.info('正在读取yaml文件...')
yaml_data = dl_yaml.DLYaml().load_yaml('env.yaml')
print(yaml_data)
except Exception as e:
logging.exception('yaml文件读取失败:',e)