python接口自动化测试二十九:yaml配置文件的写和读

# 先安装ruamel.yaml模块

写入配置文件:

import os
# 先安装ruamel.yaml模块
from ruamel import yaml

# 将字典写入到yaml
dict = {
'host1': '123',
'host2': '456',
'host3': '789',
'host4': '147',
'host5': {'asd': '123'}
}

curpath = os.path.dirname(os.path.realpath(__file__))
yamlpath = os.path.join(curpath, "config.yaml")

# 写入到yaml文件
with open(yamlpath, "w", encoding="utf-8") as f:
yaml.dump(dict, f, Dumper=yaml.RoundTripDumper)


读取配置文件

 

# 读取yaml文件
# a = open(yamlpath, "r")
# print(yaml.load(a.read(), Loader=yaml.Loader))

a = yaml.load(open(yamlpath, "r").read(), Loader=yaml.Loader)
print(a)
print(a['host1'])


封装起来方便以后调用

import os
from ruamel import yaml

def getConfig():
# 读取yaml文件中的PC投资端的host
curpath = os.path.dirname(os.path.realpath(__file__))
yamlpath = os.path.join(curpath, "config.yaml")
a = yaml.load(open(yamlpath, "r").read(), Loader=yaml.Loader)
return a

if __name__=="__main__":
print(getConfig()['XXX'])

 

from common.get_config import getConfig
host = getConfig()['host_PC_TZ']

print(host)

转载于:https://www.cnblogs.com/malinalian/p/10491867.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值