python读取测试数据的多种方式

本文主要介绍了python读取测试数据的多种方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、通过创建.ini或.conf文件读取

1、创建一个config.ini或者.conf文件,这种方法就是ini文件的读取,如下

 [api] url = www.taobao.com method = get [mysql] db=hello port=3306 

2、使用python的configparser库读取,代码如下

 from configparser import ConfigParser import os conn=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 yaml def read_file(file_path): with open(file_path, 'r', encoding='utf-8') as f: res = yaml.load(f,Loader=yaml.FullLoader) return res if __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 pandas def 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_list if __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读取测试数据内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是python读取测试数据的多种方式的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python