Bootstrap

Python+Excel接口测试

Python接口测试需求

自动读取Excel表格中的接口测试用例,执行从Excel中读取的用例,接口测试完成后,发送邮件通知。
整个测试过程实现步骤:
1、编写Excel测试用例模板
2、读取Excel表格中的测试用例
3、组装URL、请求参数
4、发送请求
5、整理测试结果并发送邮件

1、编写Excel测试用例模板

Excel编写的测试用例没有固定的模板,用例的格式根据实际测试需求或者接口的字段来确定所需要的格式
测试用例模板

读取Excel中的用例

使用Python对Excel操作时,需要安装xlrd

def test_case_in_excel(test_case_file):
    test_case_file = os.path.join(os.getcwd(), test_case_file)      # 获取测试用例的路径
    if not os.path.exists(test_case_file):
        Logger().info('测试用例excel文件不存在或路径有误!')
        sys.exit()      # 找不到指定测试文件,就退出程序 os.system("exit")是用来退出cmd的
    test_case = xlrd.open_workbook(test_case_file)      # 读取excel文件
    table = test_case.sheets()[0]       # 获取第一个sheet,下表从0开始
    error_case = []     # 记录错误用例
    '''读取表格中的用例,其实就像一个二维数组'''
    for i in range(1, table.nrows):
        api_id = str(int(table.cell_value(i, 0))).replace("\n", "").replace("\r", "")
        api_name = table.cell_value(i, 1).replace("\n", "").replace("\r", "")
        api_host = table.cell_value(i, 2).replace("\n", "").replace("\r", "")
        api_url = table.cell_value(i, 3).replace("\n", "").replace("\r", "")
        api_method = table.cell_value(i, 4).replace("\n", "").replace("\r", "")
        api_data_type = table.cell_value(i, 5).replace("\n", "").replace("\r", "")
        api_request_data = table.cell_value(i, 6).replace("\n", "").replace("\r", "")
        api_check_point = table.cell_value(i, 7).replace("\n", "").replace("\r", "")
        try:
            # 调用接口请求方法
            status, res = interface_test(api_id, api_name, api_host, api_url, api_method, api_data_type, api_request_data, api_check_point)
            if status != 200:
                # append()只接受一个参数,所以四个参数要用一个括号括起来
                # 请求失败,则向error_case中增加一条记录
                error_case.append((api_id + " " + api_name, str(status), api_host + api_url))
        except Exception as e:
            Logger().error(e)
            Logger().info("第{}个接口请求失败,请检查接口是否异常.".format(api_id))
            # 访问异常,则向error_case中增加一条记录
            error_case.append((api_id + " " + api_name, "请求失败", api_host + api_url))
    return error_case

组装URL、参数并发送请求

根据Method字段来判断,是使用POST还是GET方式请求
根据Request_Data_Type字段来判断参数传递方式,选择不同的参数处理方式
根据Request_Data字段来判断是否带参数,选择不同的请求处理方式
def interface_test(api_id, api_name, api_host, api_url, api_method, api_data_type, api_request_data, api_check_point):
    # 构造请求头headers
    headers = {
        "Content-Type": "application/json"
    }
    # 判断请求方式,若是GET,调用get请求;若是POST,调用post请求
    if api_method == "GET":
        # 判断get请求是否带参数,然后选择不同get请求方式
        if bool(api_request_data) == False:
            res = requests.get(url=api_host + api_url)
            status_code = res.status_code
            if status_code == 200:
                Logger().info("第{}条用例-{}-执行成功,状态码为:{},结果返回值为--{}.".format(api_id, api_name, status_code, res.text))
            else:
                Logger().error("第{}条用例-{}-执行失败!!!错误返回码:{}".format(api_id, api_name, status_code))
        else:
            res = requests.get(url=api_host + api_url, params=api_request_data)
            status_code = res.status_code
            if status_code == 200:
                Logger().info("第{}条用例-{}-执行成功,状态码为:{},结果返回值为--{}.".format(api_id, api_name, status_code, res.text))
            else:
                Logger().error("第{}条用例-{}-执行失败!!!错误返回码:{}".format(api_id, api_name, status_code))
    elif api_method == "POST":
        
;