增加了入参关键字,关键字参数

This commit is contained in:
zy7y 2020-08-09 06:40:54 +08:00
parent ee0cfa7456
commit 23558dc2a4
3 changed files with 44 additions and 25 deletions

View File

@ -16,16 +16,19 @@ class BaseRequest(object):
pass
# 请求
def base_requests(self, method, url, data=None, file_var=None, file_path=None, header=None):
def base_requests(self, method, url, parametric_key, data=None, file_var=None, file_path=None, header=None):
"""
:param method: 请求方法
:param url: 接口path
:param data: 数据,请传入dict样式的字符串
:param file_path: 上传的文件路径
:param file_var: 接口中接收文件对象的参数名
:param url: 请求url
:param parametric_key: 入参关键字 get/delete/head/options/请求使用params,
post/put/patch请求可使用jsonapplication/json/data
:param data: 参数数据默认等于None
:param file_var: 接口中接受文件的参数关键字
:param file_path: 文件对象的地址
:param header: 请求头
:return: 完整的响应对象
:return: 返回json格式的响应
"""
session = requests.Session()
if (file_var in [None, '']) and (file_path in [None, '']):
@ -33,11 +36,23 @@ class BaseRequest(object):
else:
# 文件不为空的操作
files = {file_var: open(file_path, 'rb')}
# get 请求参数传递形式 params
if method == 'get':
# # get 请求参数传递形式 params
# if method == 'get':
# res = session.request(method=method, url=url, params=data, headers=header)
# else:
# res = session.request(method=method, url=url, data=data, files=files, headers=header)
# logger.info(f'请求方法:{method},请求路径:{url}, 请求参数:{data}, 请求文件:{files}, 请求头:{header})')
# return res.json()
if parametric_key == 'params':
res = session.request(method=method, url=url, params=data, headers=header)
else:
elif parametric_key == 'data':
res = session.request(method=method, url=url, data=data, files=files, headers=header)
elif parametric_key == 'json':
res = session.request(method=method, url=url, json=data, files=files, headers=header)
else:
raise ValueError('可选关键字为get/delete/head/options/请求使用params, post/put/patch请求可使用jsonapplication/json/data')
logger.info(f'请求方法:{method},请求路径:{url}, 请求参数:{data}, 请求文件:{files}, 请求头:{header})')
return res.json()

View File

@ -84,15 +84,15 @@ class TestApiAuto(object):
logger.debug(f'data有数据依赖无数据 {data}')
return data, header
@pytest.mark.parametrize('case_number,path,is_token,method,file_var,'
'file_path,dependent,data,expect,actual', data_list, ids=title_ids)
def test_main(self, case_number, path, is_token, method, file_var, file_path,
@pytest.mark.parametrize('case_number,path,is_token,method,parametric_key,file_var,'
'file_path, parameters, dependent,data,expect,actual', data_list, ids=title_ids)
def test_main(self, case_number, path, is_token, method, parametric_key, file_var, file_path, parameters,
dependent, data, expect, actual):
logger.warning(f"=========用例编号:{case_number}===========开始运行=====\n")
with allure.step("处理相关数据依赖header"):
data, header = self.treating_data(is_token, dependent, data)
with allure.step("发送请求取得响应结果的json串"):
res = br.base_requests(method=method, url=base_url + path, file_var=file_var, file_path=file_path,
res = br.base_requests(method=method, url=base_url + path, parametric_key=parametric_key, file_var=file_var, file_path=file_path,
data=data, header=header)
with allure.step("将响应结果的内容写入实际响应字典/excel实际结果栏中"):
# ReadData(case_data_path).write_result(case_number, res) # 向excel对应case中写入实际响应
@ -109,7 +109,7 @@ class TestApiAuto(object):
expect = eval(expect)
with allure.step("预期结果与实际响应进行断言操作"):
assert really == expect
logger.info(f'完整的json响应: {res}\n 需要校验的数据字典: {really}\n 预期校验的数据字典: {expect}\n 测试结果: {really == expect}')
logger.info(f'完整的json响应: {res}\n 需要校验的数据字典: {really}\n 预期校验的数据字典: {expect}\n 测试结果: {really == expect}\n')
if __name__ == '__main__':

View File

@ -23,7 +23,7 @@ class ReadData(object):
def get_data(self):
"""
:return:
:return: data_list - pytest参数化可用的数据 title_list pytest参数化 ids关键字用到的标题数据
"""
data_list = []
title_list = []
@ -41,13 +41,17 @@ class ReadData(object):
path = table.cell_value(norw, 2)
is_token = table.cell_value(norw, 4)
method = table.cell_value(norw, 5)
file_var = table.cell_value(norw, 6)
file_path = table.cell_value(norw, 7)
dependent = table.cell_value(norw, 8)
data = table.cell_value(norw, 9)
expect = table.cell_value(norw, 10)
actual = table.cell_value(norw, 11)
value = [case_number, path, is_token, method, file_var, file_path, dependent, data, expect, actual]
# 入参关键字
parametric_key = table.cell_value(norw, 6)
file_var = table.cell_value(norw, 7)
file_path = table.cell_value(norw, 8)
# 路径参数
parameters = table.cell_value(norw, 9)
dependent = table.cell_value(norw, 10)
data = table.cell_value(norw, 11)
expect = table.cell_value(norw, 12)
actual = table.cell_value(norw, 13)
value = [case_number, path, is_token, method, parametric_key, file_var, file_path, parameters, dependent, data, expect, actual]
logger.info(value)
# 配合将每一行转换成元组存储,迎合 pytest的参数化操作如不需要可以注释掉 value = tuple(value)
value = tuple(value)
@ -107,8 +111,8 @@ class ReadData(object):
if __name__ == '__main__':
rd = ReadData('../data/case_data.xlsx')
data, nlen, title = rd.get_data()
print(nlen)
data, title = rd.get_data()
# value = "{'data': {'id': 500, 'rid': 0, 'username': 'admin', 'mobile': '18822222223', 'email': '12344@qq.com', 'token': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTYzNTgxODUsImV4cCI6MTU5NjQ0NDU4NX0.utWSoAxiWCbf9W1xCkGo2669g9VR5zGMgcsbgblShrs'}, 'meta': {'msg': '登录成功', 'status': 200}}"
# rd.write_result(2,11,value)