自动化测试框架(二)读取配置文件中内容

@[TOC]python接口自动化框架

通过ConfigParser模式读取配置文件.ini

读取配置文件中内容

配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

'''
[mysql]
host=api.lemonban.com
database=futureloan
port=3306
user=future
passwd=123456
'''
import os
from common.my_path import conf_dir
from configparser import ConfigParser
class MyConf(ConfigParser):
    def __init__(self,filename):
        super().__init__()
        # 读取配置文件
        self.read(filename,encoding="utf-8")

通过get()\getint()等获取配置文件中的值

print('------------get方式获取某个key对应值----------------')
print(MyConf(os.path.join(conf_dir, "conf.ini")).get("server", "host"))
print(MyConf(os.path.join(conf_dir, "mysql.ini")).getint("mysql", "port"))
print('------------key----------------')
a=MyConf(os.path.join(conf_dir, "mysql.ini"))
for key in a["mysql"]:
    print(key)
print('------------key,value----------------')
for key ,value in a["mysql"].items():
    print(key,"=",value)

输出

------------get方式获取某个key对应值----------------
http://api.lemonban.com/futureloan/
3306
------------key----------------
host
database
port
user
passwd
------------key,value----------------
host = api.lemonban.com
database = futureloan
port = 3306
user = future
passwd = 123456

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值