python 接口测试读取excel多行数据

         python接口测试遇到多个字段,且需要参数化,需要借助excel编辑,然后读取。用到python的xlrd和requests库import xlrd

 1 import xlrd 
 2 
 3 def xlrd_read_body(): 
    table = xlrd.open_workbook(r'xxx.xlsx').sheet_by_index(0)
4 body_list = [] #空列表,用于存放表格所有组数据 5 body_loop = 1 #用于判断退出循环 6 while True: 7 body_data = {} # 空字典,用于存放每一组数据 8 for i in range(table.ncols): 9 body_data[table.cell(0,i).value] = table.cell(body_loop,i).value #获取一组数据 10 body_list.append(body_data) 11 body_loop += 1 12 if body_loop >= table.nrows: #大于表格的总行数就退出循环 13 break 14 return body_list

    然后返回的body_list是一个列表,我们需要从其中遍历到我们接口参数去,从而实现参数化,以后需要修改直接修改excel表格就可以了。

 

原文地址:https://www.cnblogs.com/shiyuheng/p/10370436.html