python读取测试数据的多种方式,久久派带你了解更多相关信息。
目录
- 一、通过创建.ini或.conf文件读取
- 二、通过yaml文件读取
- 三、通过excel读取
一、通过创建.ini或.conf文件读取
1、创建一个config.ini或者.conf文件,这种方法就是ini文件的读取,如下
[api]url = www.taobao.commethod = get[mysql]db=helloport=3306
2、使用python的configparser库读取,代码如下
from configparser import ConfigParserimport osconn=ConfigParser()#获取ini文件的路径file_path = os.path.join(os.path.abspath(\'.\'),\'config.ini\')conn.read(file_path)url=conn.get(\'api\',\'url\')method=conn.get(\'api\',\'method\')port=conn.get(\'mysql\',\'port\')print(url,method,port)
3、运行得到如下的值,读取成功
www.taobao.com get 3306
二、通过yaml文件读取
1、首先创建一个.yaml文件,这里我创建login_data.yaml,具体数据格式大家自行google,这里我随便编写一些数据
- caseid: 1 method: post title: 正常登录 url: api/v1/login data: username: test password: 123456 headers: Content-Type: application/json User-Agent: Mozilla/5.0 expected: 0
2、命令行通过pip install pyyaml安装yaml包,直接上代码
import yamldef read_file(file_path): with open(file_path, \'r\', encoding=\'utf-8\') as f: res = yaml.load(f,Loader=yaml.FullLoader) return resif __name__ == \'__main__\': d = read_file(r\'..\\test_data\\login\\login_data.yml\') print(d)
3、运行结果如下,是一个列表
[{\’caseid\’: 1, \’method\’: \’post\’, \’title\’: \’正常登录\’, \’url\’: \’api/v1/login\’, \’data\’: {\’username\’: \’test\’, \’password\’: 123456}, \’headers\’: {\’Content-Type\’: \’application/json\’, \’User-Agent\’: \’Mozilla/5.0\’}, \’expected\’: 0}]
三、通过excel读取
1、创建一个excel文件,随便写点数据
2、通过pandas读取,当然还有其他的很多库,本人觉得这个最方便,直接转化成dict方便使用
import pandasdef excel_to_dic(filename): df = pandas.read_excel(filename) data_list = [] for i in df.index.values: data = df.iloc[i, [j for j in range(len(df.keys()))]].to_dict() data_list.append(data) return data_listif __name__ == \'__main__\': file = r\'C:\\aaa.xlsx\'#之前创建的文件地址 data = excel_to_dic(file) print(data)
3、运行结果如下
[{\’name\’: \’zhangsan\’, \’phone\’: 13112345678, \’age\’: 20}, {\’name\’: \’lisi\’, \’phone\’: 13867543678, \’age\’: 30}, {\’name\’: \’wangwu\’, \’phone\’: 15967845632, \’age\’: 40}]
可以看出结果是把excel表中的每一行数据转换成了一个dict,更加方便以后使用!!!
到此这篇关于python读取测试数据的多种方式的文章就介绍到这了,更多相关python读取测试数据内容请搜索趣讯吧以前的文章或继续浏览下面的相关文章希望大家以后多多支持趣讯吧!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/10912.html