问题:从excel中读取的数据应是什么格式呢?
1pytest中需要参数化时,需要的[(valuea,valueb)(valuex,valuey)]------------- 列表内是元祖
import pytest def add(x, y): return x + y @pytest.mark.parametrize("x, y, expected", [ (1, 1, 2), (2, 3, 5), (-1, 1, 0), ]) def test_add(x, y, expected): result = add(x, y) assert result == expected
结论:从上面例子看出,需要准备的数据的格式【(指定列值1,指定列值2),(指定列值1,指定列值2)】
2 当传递 预期结果 请求体 这些参数key时,excel要根据key给出对应的value值------列表内是字典
def get_data(*args):#["a",'b']
for one in args: print("one:",one)
#one: ['a', 'b'] def get_key(*keys):#"x","y"
dict = {} for key in keys: dict[key]="value"
return dict # {'x': 'value', 'y': 'value'}
if __name__ == '__main__':
get_data(["a",'b'])
d=get_key("x","y")
print(d)
代码示例
第一步:读excel表 需要两个方法一个[(),()];另一个[{},{}]
import xlrd import json from configs.configs import HOST from utils.md5 import get_md5 # 在创建excel时,将登录接口的返回结果 粘贴到excel时需要 “只粘贴文本” # 将excel 实例化 def get_excel_data(*keys): excel_data=[]# [{值},{值}] wookbook=xlrd.open_workbook('D:/python-pro/py0803/data/test_case.xls') # 通过名字读取sheet值 sheet=wookbook.sheet_by_name('Sheet1') # 获得标题存在header中 header = sheet.row_values(0) # ['url', 'title', 'method', 'body', 'expect'] 也就是row=0 第0行的值,标题 # 获得列表的行总数 nrows=sheet.nrows # print(nrows)# 3 ,其中 标题一行,数据两行 # 循环读取每行的数据 for i in range(1,nrows): # 每行都是一个字典 data_dict 注意这里是全部列的数据 data_dict_row=dict(zip(sheet.row_values(0),sheet.row_values(i))) # 将包含\n换行符的字符串做处理 for key in data_dict_row: # 遍历全部的单元格内的值 value=data_dict_row[key] if isinstance(value,str): #是否是字符串?是的话,做处理 data_dict_row[key]=value.replace('\n','').replace("\\",'') # 根据传入的参数 筛选出目标列 放到一个新的字典中 row_data = {} for key in keys: if key in header: # 当传参是excel标题没有的时 不做处理 if key == 'url': # 如果是url 需要拼 row_data[key]=f"{HOST}{data_dict_row['url']}" elif key == 'body': # 请求体 需要psw处理md5 temp = eval(data_dict_row['body'])# 转字符串为字典 temp['password']=get_md5(temp['password']) row_data[key]=temp else: row_data[key]=data_dict_row[key] excel_data.append(row_data) return excel_data def get_excel_lis(*keys): excel_lis_dic=get_excel_data(*keys) exc_list_list=[] for one in excel_lis_dic:# 遍历列表的第一个字典 excel_col_value= [] # 定义一个存放 每行 列值的地方 for value in one.values(): # 从字典取出对应的值 excel_col_value.append(value) # 將value放到数组中 exc_list_list.append(tuple(excel_col_value)) return exc_list_list if __name__ == '__main__': # d=get_excel_data("method","url","body","expect") dd=get_excel_lis("method","url","body","expect") print(dd)
第二步 登录方法
# D:\python-pro\py0803\libs\login.py # md5 加密 import json import requests from utils.read_excel import get_excel_data def get_login(method,url,data): resp=getattr(requests,method.lower())(url,data) resp_dic=eval(resp.text.replace('false', 'False')) # 从字符转字典 return resp_dic if __name__ == '__main__': for one in get_excel_data("method","url","body"): d=get_login(one['method'],one['url'],one['body']) print(d['msg'])
第三步: pytest 断言
# D:\python-pro\py0803\libs\login.py # 比较 请求的结果和 excel中预期的结果 import pytest from libs.login import get_login from utils.read_excel import get_excel_lis import pdb class TestLogin: @pytest.mark.parametrize('method,url,body,expect', get_excel_lis("method","url","body","expect")) def test_real_exp(self,method,url,body,expect): real = get_login(method,url,body)["msg"] print(real) exp = eval(expect)['msg'] print(exp) assert real == exp if __name__ == '__main__': pytest.main(['-vs'])
标签:__,get,excel,读数据,key,格式,data,row From: https://www.cnblogs.com/haha1988/p/17605547.html