首页 > 其他分享 >pytest + excel参数驱动

pytest + excel参数驱动

时间:2022-11-28 12:13:13浏览次数:37  
标签:name excel list value range pytest 驱动 logger

背景

最近紧急支持一个接口测试,打算把接口的参数都放到execl中维护,且一个接口需要请求两次。

思路

1.execl中构造参数

接口需要请求两次或者多次,比较两次后台返回的结果,那么对应的execl中的数据下图:
image

红色的代表第一次请求参数和请求结果,蓝色的代表第二次请求参数和结果;

2.pytest接受excel中的数据

2.1 先用openpyxl读取excel数据

image

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
=================================================
操作excel
==================================================
"""
from public.public_logger import get_logger
from openpyxl import load_workbook
from public.global_public_fun import case_list_file_path
logger = get_logger()


# 读取execl数据
def read_excel_fun(start: int, end: int) -> list:
    logger.info('读取execl中的数据')
    wbook = load_workbook(filename=case_list_file_path)
    wsheet = wbook['Sheet1']
    # 获取key
    name_range = wsheet["B2":"Y2"]
    list_name = []
    for x in name_range[0]:
        # print(x.value)
        list_name.append(x.value)
    # 获取值
    all_list = []
    for x in range(start, end):
        dict1 = {}
        list_value = []
        cell_range = wsheet["B%s" % x:"Y%s" % x]
        # print(cell_range)
        for y in cell_range[0]:
            # print(y.value)
            list_value.append(y.value)
        for x in range(len(list_name)):
            dict1[list_name[x]] = list_value[x]
        # print(dict1)
        all_list.append(dict1)
        # print(all_list)
        logger.info(all_list)
    wbook.save(filename=case_list_file_path)
    return all_list


if __name__ == '__main__':
    read_excel_fun()

标签:name,excel,list,value,range,pytest,驱动,logger
From: https://www.cnblogs.com/tarzen213/p/16931830.html

相关文章