forked from DxvLwRYF/apiAutoTest
优化日志打印
This commit is contained in:
parent
9708e0cdc2
commit
1aef990e1e
|
@ -9,7 +9,6 @@
|
|||
"""
|
||||
import json
|
||||
import shutil
|
||||
|
||||
import jsonpath
|
||||
from loguru import logger
|
||||
import pytest
|
||||
|
@ -20,6 +19,7 @@ from tools.read_config import ReadConfig
|
|||
from tools.read_data import ReadData
|
||||
from tools.save_response import SaveResponse
|
||||
|
||||
# 读取配置文件 对象
|
||||
rc = ReadConfig()
|
||||
base_url = rc.read_serve_config('dev')
|
||||
token_reg, res_reg = rc.read_response_reg()
|
||||
|
@ -31,22 +31,21 @@ report_zip = rc.read_file_path('report_zip')
|
|||
email_setting = rc.read_email_setting()
|
||||
# 实例化存响应的对象
|
||||
save_response_dict = SaveResponse()
|
||||
# 读取excel数据对象
|
||||
data_list, title_ids = ReadData(case_data_path).get_data()
|
||||
|
||||
# 数据处理对象
|
||||
treat_data = TreatingData()
|
||||
|
||||
# 请求对象
|
||||
br = BaseRequest()
|
||||
|
||||
|
||||
class TestApiAuto(object):
|
||||
def setup(self):
|
||||
logger.info('用例运行开始=====')
|
||||
|
||||
@pytest.mark.parametrize('case_number,path,is_token,method,parametric_key,file_var,'
|
||||
'file_path, parameters, dependent,data,expect', 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):
|
||||
logger.debug(f'执行用例编号:{case_number}⬇️⬇️⬇️')
|
||||
with allure.step("处理相关数据依赖,header"):
|
||||
data, header, parameters_path_url = treat_data.treating_data(is_token, parameters, dependent, data, save_response_dict)
|
||||
with allure.step("发送请求,取得响应结果的json串"):
|
||||
|
@ -54,7 +53,6 @@ class TestApiAuto(object):
|
|||
data=data, header=header)
|
||||
with allure.step("将响应结果的内容写入实际响应字典/excel实际结果栏中"):
|
||||
save_response_dict.save_actual_response(case_key=case_number, case_response=res)
|
||||
|
||||
# 写token的接口必须是要正确无误能返回token的
|
||||
if is_token == '写':
|
||||
with allure.step("从登录后的响应中提取token到header中"):
|
||||
|
@ -64,21 +62,23 @@ class TestApiAuto(object):
|
|||
with allure.step("处理读取出来的预期结果响应"):
|
||||
expect = json.loads(expect)
|
||||
with allure.step("预期结果与实际响应进行断言操作"):
|
||||
assert really == expect
|
||||
logger.info(f'完整的json响应: {res}\n 需要校验的数据字典: {really}\n 预期校验的数据字典: {expect}\n 测试结果: {really == expect}\n')
|
||||
try:
|
||||
assert really == expect
|
||||
logger.info(f'完整的json响应: {res}\n需要校验的数据字典: {really} 预期校验的数据字典: {expect} \n测试结果: {really == expect}')
|
||||
except AssertionError as e:
|
||||
logger.error(f'用例执行不通过.{e}')
|
||||
logger.debug(f'用例编号:{case_number},执行完毕,日志查看⬆️⬆️⬆️')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import os
|
||||
|
||||
if os.path.exists('../report') and os.path.exists('../log'):
|
||||
shutil.rmtree(path='../report')
|
||||
shutil.rmtree(path='../log')
|
||||
logger.add(log_path)
|
||||
|
||||
pytest.main(args=[f'--alluredir={report_data}'])
|
||||
os.system(f'allure generate {report_data} -o {report_generate} --clean')
|
||||
logger.debug('报告已生成')
|
||||
logger.warning('报告已生成')
|
||||
|
||||
# 使用jenkins集成将不会使用到这两个方法
|
||||
# from tools.zip_file import zipDir
|
||||
|
|
|
@ -29,7 +29,7 @@ class TreatingData(object):
|
|||
header = self.no_token_header
|
||||
else:
|
||||
header = self.token_header
|
||||
logger.info(f'处理依赖前data的数据:{data} \n')
|
||||
logger.info(f'处理依赖前data的数据:{data}')
|
||||
# 处理依赖数据data
|
||||
if dependent != '':
|
||||
dependent_data = save_response_dict.read_depend_data(dependent)
|
||||
|
@ -38,18 +38,18 @@ class TreatingData(object):
|
|||
# 合并组成一个新的data
|
||||
dependent_data.update(json.loads(data))
|
||||
data = dependent_data
|
||||
logger.debug(f'data有数据,依赖有数据时 {data}')
|
||||
logger.info(f'data有数据,依赖有数据时 {data}')
|
||||
else:
|
||||
# 赋值给data
|
||||
data = dependent_data
|
||||
logger.debug(f'data无数据,依赖有数据时 {data}')
|
||||
logger.info(f'data无数据,依赖有数据时 {data}')
|
||||
else:
|
||||
if data == '':
|
||||
data = None
|
||||
logger.debug(f'data无数据,依赖无数据时 {data}')
|
||||
logger.info(f'data无数据,依赖无数据时 {data}')
|
||||
else:
|
||||
data = json.loads(data)
|
||||
logger.debug(f'data有数据,依赖无数据 {data}')
|
||||
logger.info(f'data有数据,依赖无数据 {data}')
|
||||
|
||||
# 处理路径参数Path的依赖
|
||||
# 传进来的参数类似 {"case_002":"$.data.id"}/item/{"case_002":"$.meta.status"},进行列表拆分
|
||||
|
@ -62,12 +62,12 @@ class TreatingData(object):
|
|||
path_dict = json.loads(path_list[i])
|
||||
except JSONDecodeError as e:
|
||||
# 序列化失败此path_list[i]的值不变化
|
||||
logger.debug(f'无法转换字典,进入下一个检查,本轮值不发生变化:{path_list[i]},\n {e}')
|
||||
logger.error(f'无法转换字典,进入下一个检查,本轮值不发生变化:{path_list[i]},{e}')
|
||||
# 跳过进入下次循环
|
||||
continue
|
||||
else:
|
||||
# 解析该字典,获得用例编号,表达式
|
||||
logger.error(f'{path_dict}')
|
||||
logger.info(f'{path_dict}')
|
||||
# 处理json.loads('数字')正常序列化导致的AttributeError
|
||||
try:
|
||||
for k, v in path_dict.items():
|
||||
|
@ -75,9 +75,9 @@ class TreatingData(object):
|
|||
# 尝试从对应的case实际响应提取某个字段内容
|
||||
path_list[i] = jsonpath.jsonpath(save_response_dict.actual_response[k], v)[0]
|
||||
except TypeError as e:
|
||||
logger.info(f'无法提取,请检查响应字典中是否支持该表达式,{e}')
|
||||
logger.error(f'无法提取,请检查响应字典中是否支持该表达式,{e}')
|
||||
except AttributeError as e:
|
||||
logger.error(f'类型错误:{type(path_list[i])},本此将不转换值 {path_list[i]},\n {e}')
|
||||
logger.error(f'类型错误:{type(path_list[i])},本此将不转换值 {path_list[i]},{e}')
|
||||
# 字典中存在有不是str的元素:使用map 转换成全字符串的列表
|
||||
path_list = map(str, path_list)
|
||||
|
||||
|
|
|
@ -46,10 +46,9 @@ class ReadData(object):
|
|||
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)
|
||||
value = [case_number, path, is_token, method, parametric_key, file_var, file_path, parameters, dependent, data, expect]
|
||||
# 配合将每一行转换成元组存储,迎合 pytest的参数化操作,如不需要可以注释掉 value = tuple(value)
|
||||
value = tuple(value)
|
||||
data_list.append(value)
|
||||
logger.info(f'从Excel中读取到的用例数据列表\n{data_list}\n\n')
|
||||
return data_list, title_list
|
||||
|
|
|
@ -15,7 +15,7 @@ from loguru import logger
|
|||
|
||||
class SaveResponse(object):
|
||||
def __init__(self):
|
||||
self.actual_response= {}
|
||||
self.actual_response = {}
|
||||
|
||||
# 保存实际响应
|
||||
def save_actual_response(self, case_key, case_response):
|
||||
|
|
Loading…
Reference in New Issue