configparser--配置文件分析库

configparser--配置文件分析库

一、介绍
该模块主要提供了一个类 ConfigParser,它实现对结构化的文件,类似Windows的INI的文件进行操作。通过INI文件就可以对测试框架测试数据独立出来

配置文件的格式: []包含的叫section 节点, section 下有option=value这样的键值


测试参数配置文件data.ini
[testcase1]
testcase1.1=('name1','password1','nick1',)
testcase1.2=('name2','password2','nick2')

[testcase2]
testcase2.1=('name1','password1','nick1')
testcase2.2=('name2','password2','nick2')

二、示例
取元组类型参数代码

 1 # coding: utf-8
 2 
 3 from configparser import ConfigParser
 4 # 实例化一个ConfigParser()类
 5 data = ConfigParser()
 6 # 读取文件
 7 data.read('./data/data.ini','utf-8')
 8 # 通过和section和option 取 valuate,返回unicode类型
 9 re1 = data['testcase1']['testcase1.1']
10 # 将unicode转换成string
11 # re2 = str(re1)
12 re2 = re1.encode('utf-8')
13 # 还原为元组
14 re = eval(re2)

三、封装函数

封装取测试参数函数

1 def GetIniData(filepath, section, option):
2     “”“输入ini文件路径,节点,键,取对应测试数据”“”    
3     try:
4         data = ConfigParser()
5         data.read(filepath,'utf-8')
6         r = eval(data[section][option].encode('utf-8'))
7         return r
8     except BaseException as msg:
9         return msg

猜你喜欢

转载自www.cnblogs.com/albatron/p/8946639.html