python3+pytest 接口测试:读取excel

进行接口测试时,会测单个接口的多种场景,这时我们通过加入excel进行维护。
如果增加测试场景,只需要在对应excel表中维护即可

  1. 新建读取excel表格的方法


    封装读取excel的方法
  2. 用例中调用该方法,读取表格内容


    获取sheet表内容

3.用例中使用表格数据


用例中使用

  • 附上代码

读取表格内容

excel_list = read_excel.read_excel_sheet('./document/DNC系统.xls','人员名称搜索')
ids_list = []
for i in range(len(excel_list)):
    # 删除excel_list中每个小list的最后一个元素,并赋值给ids_pop
    ids_pop = excel_list[i].pop()
    # 将ids_pop添加到 ids_list 里面
    ids_list.append(ids_pop)

封装读表方法

import xlrd
import sys

def read_excel_sheet(file, sheet_name):
    li = []
    wb = xlrd.open_workbook(filename=file)  # 打开文件
    # print(wb.sheet_names())  # 打印所有表格名字
    sheet_names = wb.sheet_names()  # 获取所有工作表名称
    # 判断输入表名是否实际存在于excel内
    if sheet_name not in sheet_names:
        print('ERROR!!!\n输入表名错误,输入表名为:{}。实际表名为{}'.format(sheet_name, sheet_names))
        sys.exit()
    sheet_data = wb.sheet_by_name(sheet_name)  # 通过工作表名称获取某张表格
    #获取表格内从第二行到最后一行的数据
    for i in range(1, sheet_data.nrows):
        # l.append(sheet1.row_values(i))
        d = []
        for j in range(sheet_data.ncols):
            d = sheet_data.row_values(i)
        li.append(d)

    # print(li)
    return li
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。