在做自动化测试的时候,经常会有一些配置文件(如:yaml)等做一些配置信息,那么python怎么读取yaml文件。
首先在项目下建一个common包,将一些公共类封装在此包下,
再在项目下见一个config包,主要放一些yaml等配置文件,yaml配置文件内容:
base_url:
address: http://127.0.0.1/api/v1.0 #测试环境地址
base_config:
login_user: userName #登录用户名
login_pwd: 123456 #登录用户名密码
user_chinese: 中文姓名
type_key: Content-Type #接口信息头传参类型对应的键
json_type: application/json;charset=UTF-8 #接口信息头json格式报文类型
form_type: application/x-www-form-urlencoded #接口信息头form格式报文类型
token_key: token
接下来我们封装读取yaml文件的类
class ConfigYaml:
def __init__(self, key=None, dir="Config", file='Config.yaml', encoding='utf-8'):
'''
:param key:
:param dir:
:param file:
:param encoding:
''&#