2020-08-03 16:15:03 +08:00
|
|
|
|
#!/usr/bin/env/python3
|
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
@project: apiAutoTest
|
|
|
|
|
@author: zy7y
|
|
|
|
|
@file: base_requests.py
|
|
|
|
|
@ide: PyCharm
|
|
|
|
|
@time: 2020/7/31
|
|
|
|
|
"""
|
|
|
|
|
|
2020-11-22 22:20:33 +08:00
|
|
|
|
import requests
|
2021-05-17 18:29:38 +08:00
|
|
|
|
from tools import allure_step, allure_title, logger, allure_step_no
|
2020-11-20 20:09:12 +08:00
|
|
|
|
from tools.data_process import DataProcess
|
2020-11-19 00:49:26 +08:00
|
|
|
|
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
|
|
|
|
class BaseRequest(object):
|
2020-11-22 22:20:33 +08:00
|
|
|
|
session = None
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
2020-11-22 22:20:33 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_session(cls):
|
2021-05-17 18:29:38 +08:00
|
|
|
|
"""
|
|
|
|
|
单例模式保证测试过程中使用的都是一个session对象
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2020-11-22 22:20:33 +08:00
|
|
|
|
if cls.session is None:
|
|
|
|
|
cls.session = requests.Session()
|
|
|
|
|
return cls.session
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def send_request(cls, case: list, env: str = 'dev') -> object:
|
|
|
|
|
"""处理case数据,转换成可用数据发送请求
|
|
|
|
|
:param case: 读取出来的每一行用例内容,可进行解包
|
|
|
|
|
:param env: 环境名称 默认使用config.yaml server下的 dev 后面的基准地址
|
|
|
|
|
return: 响应结果, 预期结果
|
2020-08-03 16:15:03 +08:00
|
|
|
|
"""
|
2021-05-19 12:31:25 +08:00
|
|
|
|
case_number, case_title, header, path, method, parametric_key, file_obj, data, extra, sql, expect = case
|
2021-04-19 13:03:55 +08:00
|
|
|
|
logger.debug(
|
2021-05-19 12:31:25 +08:00
|
|
|
|
f"用例进行处理前数据: \n 接口路径: {path} \n 请求参数: {data} \n 提取参数: {extra} \n 后置sql: {sql} \n 预期结果: {expect} \n ")
|
2020-11-22 22:20:33 +08:00
|
|
|
|
# allure报告 用例标题
|
|
|
|
|
allure_title(case_title)
|
|
|
|
|
# 处理url、header、data、file、的前置方法
|
2021-05-17 18:29:38 +08:00
|
|
|
|
url = DataProcess.handle_path(path, env)
|
2021-05-04 12:34:00 +08:00
|
|
|
|
header = DataProcess.handle_header(header)
|
2020-11-22 22:20:33 +08:00
|
|
|
|
data = DataProcess.handle_data(data)
|
2021-05-17 18:29:38 +08:00
|
|
|
|
allure_step('请求数据', data)
|
2020-11-22 22:20:33 +08:00
|
|
|
|
file = DataProcess.handler_files(file_obj)
|
|
|
|
|
# 发送请求
|
2021-05-17 18:29:38 +08:00
|
|
|
|
response = cls.send_api(url, method, parametric_key, header, data, file)
|
2021-05-19 12:31:25 +08:00
|
|
|
|
# 提取参数
|
|
|
|
|
DataProcess.handle_extra(extra, response)
|
2021-05-17 18:29:38 +08:00
|
|
|
|
return response, expect, sql
|
2020-08-03 16:15:03 +08:00
|
|
|
|
|
2020-11-22 22:20:33 +08:00
|
|
|
|
@classmethod
|
2021-04-19 13:03:55 +08:00
|
|
|
|
def send_api(
|
|
|
|
|
cls,
|
|
|
|
|
url,
|
|
|
|
|
method,
|
|
|
|
|
parametric_key,
|
|
|
|
|
header=None,
|
|
|
|
|
data=None,
|
2021-05-17 18:29:38 +08:00
|
|
|
|
file=None) -> dict:
|
2020-11-22 22:20:33 +08:00
|
|
|
|
"""
|
2020-08-03 16:15:03 +08:00
|
|
|
|
:param method: 请求方法
|
2020-08-09 06:40:54 +08:00
|
|
|
|
:param url: 请求url
|
2021-01-14 14:58:06 +08:00
|
|
|
|
:param parametric_key: 入参关键字, params(查询参数类型,明文传输,一般在url?参数名=参数值), data(一般用于form表单类型参数)
|
|
|
|
|
json(一般用于json类型请求参数)
|
2020-08-09 06:40:54 +08:00
|
|
|
|
:param data: 参数数据,默认等于None
|
2020-11-22 22:20:33 +08:00
|
|
|
|
:param file: 文件对象
|
2020-08-03 16:15:03 +08:00
|
|
|
|
:param header: 请求头
|
2020-11-22 22:20:33 +08:00
|
|
|
|
:return: 返回res对象
|
2020-08-03 16:15:03 +08:00
|
|
|
|
"""
|
2020-11-22 22:20:33 +08:00
|
|
|
|
session = cls.get_session()
|
2020-11-20 20:09:12 +08:00
|
|
|
|
|
2020-08-09 06:40:54 +08:00
|
|
|
|
if parametric_key == 'params':
|
2021-04-19 13:03:55 +08:00
|
|
|
|
res = session.request(
|
|
|
|
|
method=method,
|
|
|
|
|
url=url,
|
|
|
|
|
params=data,
|
|
|
|
|
headers=header)
|
2020-08-09 06:40:54 +08:00
|
|
|
|
elif parametric_key == 'data':
|
2021-04-19 13:03:55 +08:00
|
|
|
|
res = session.request(
|
|
|
|
|
method=method,
|
|
|
|
|
url=url,
|
|
|
|
|
data=data,
|
|
|
|
|
files=file,
|
|
|
|
|
headers=header)
|
2020-08-09 06:40:54 +08:00
|
|
|
|
elif parametric_key == 'json':
|
2021-04-19 13:03:55 +08:00
|
|
|
|
res = session.request(
|
|
|
|
|
method=method,
|
|
|
|
|
url=url,
|
|
|
|
|
json=data,
|
|
|
|
|
files=file,
|
|
|
|
|
headers=header)
|
2020-08-09 06:40:54 +08:00
|
|
|
|
else:
|
2020-11-19 00:49:26 +08:00
|
|
|
|
raise ValueError(
|
2021-01-14 14:58:06 +08:00
|
|
|
|
'可选关键字为params, json, data')
|
2021-05-17 18:29:38 +08:00
|
|
|
|
response = res.json()
|
2021-04-19 13:03:55 +08:00
|
|
|
|
logger.info(
|
2021-05-17 18:29:38 +08:00
|
|
|
|
f'\n最终请求地址:{res.url}\n请求方法:{method}\n请求头:{header}\n请求参数:{data}\n上传文件:{file}\n响应数据:{response}')
|
|
|
|
|
allure_step_no(f'响应耗时(s): {res.elapsed.total_seconds()}')
|
|
|
|
|
allure_step('响应结果', response)
|
|
|
|
|
return response
|