本文主题为测试数据放在哪里
从excel,json,yarml文件中读取
1 excel文件
参考:一篇文章告诉你Python接口自动化测试中读取Text,Excel,Yaml文件的方法 - 骑着乌龟赶猪 - 博客园
编号 | 接口方式 | host | parameter | result |
1 | GET | https://sso.kkk.com | {"user":"u1","pwd":"123"} | 空 |
2 | GET | https://sso.kkk.com | {"user":"u1","pwd":"124"} | 空 |
3 | GET | https://sso.kkk.com | {"user":"u1","pwd":"125"} | 空 |
import xlrd
class ReadExcel:
excel = xlrd.open_workbook("xdata.xlsx")
sheet = excel.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
def read_excel_data(self):
first_row = self.sheet.row_values(0)
print(first_row)
self.result = []
for i in range(1, self.rows):
info_dict = {}
for j in range(0, self.cols):
info_dict[first_row[j]] = self.sheet.row_values(i)[j]
self.result.append(info_dict)
print(self.result)
if __name__ == "__main__":
ex = ReadExcel()
ex.read_excel_data()
执行结果
python test_excel.py
['编号', '接口方式', 'host', 'parameter', 'result']
[{'编号': 1.0, '接口方式': 'GET', 'host': 'https://sso.kkk.com', 'parameter': '{"user":"u1","pwd":"123"}', 'result': '空'}, {'编号': 2.0, '接口方
式': 'GET', 'host': 'https://sso.kkk.com', 'parameter': '{"user":"u1","pwd":"124"}', 'result': '空'}, {'编号': 3.0, '接口方式': 'GET', 'host': 'ht
tps://sso.kkk.com', 'parameter': '{"user":"u1","pwd":"125"}', 'result': '空'}]
【问题1】xlrd.biffh.XLRDError: Excel xlsx file; not supported
【解决方法】pycharm的terminal窗口执行命令pip install xlrd==1.2.0
2 json文件
参考:python读取json文件_Enjoy lab and life的博客-CSDN博客_python读取本地json文件
[{
"fontFamily": "微软雅黑",
"fontSize": 12,
"BaseSettings": {
"font": 1,
"size": 2
}
},
{
"fontFamily": "微黑",
"fontSize": 22,
"BaseSettings": {
"font": 2,
"size": 3
}
}
]
#filename=test_json.py
import json
f = open("testjson.json",encoding="utf-8")
str = json.load(f)
print(str)
执行结果
python test_json.py
[{'fontFamily': '微软雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': 2}}, {'fontFamily': '微黑', '
fontSize': 22, 'BaseSettings': {'font': 2, 'size': 3}}]
3 yarml文件
yarml文件相较于从excel全部读取字符串的方式更易处理,相较于json文件,可读性更高。
3.1 yarml语法
大小写敏感
缩进表示层级,不允许用Tab,只允许用空格缩进,空格的数量不重要
#表示注释
3.2 yarml数据类型
对象:键值对。转化为python的字典类型
数组:用横杠加空格表示元素。转化为python的列表
纯量(scalars):不可再分,包括:字符串,布尔值,整数,浮点数,null,时间,日期。转化为python的字典
3.3 解析yaml文件
准备工作:在pycharm的terminal窗口安装pyyaml:pip install pyyaml
#filename=testyaml.yaml
- Cat
- Dog
- Goldfish
#filename=test_yaml.py
import yaml
open_file = open("testyaml.yaml", "r", encoding="utf-8")
read_res = open_file.read()
py_res = yaml.load(read_res, Loader=yaml.FullLoader)
print(py_res)
执行结果
python test_yaml.py
['Cat', 'Dog', 'Goldfish']