优化allure报告对请求头及请求体的json支持
This commit is contained in:
parent
e8452bdce0
commit
03bb1d4a00
|
@ -133,37 +133,37 @@ class BaseApi:
|
|||
duration = end_time - start_time # 计算请求时长
|
||||
|
||||
# 记录请求时的详情信息
|
||||
r_uri = response.request.url
|
||||
r_method = method.upper()
|
||||
r_headers = response.request.headers
|
||||
r_uri = BaseApi.get_request_url(response)
|
||||
r_method = BaseApi.get_request_method(response)
|
||||
r_headers = BaseApi.get_request_headers(response)
|
||||
r_body = BaseApi.get_request_body(response)
|
||||
r_curl = BaseApi.request_to_curl(response)
|
||||
r_respone = BaseApi.get_json(response)
|
||||
r_response = BaseApi.get_json(response)
|
||||
r_duration = duration
|
||||
r_respone_status_code = response.status_code
|
||||
r_respone_headers = response.headers
|
||||
r_response_status_code = BaseApi.get_status_code(response)
|
||||
r_response_headers = BaseApi.get_response_headers(response)
|
||||
_log_msg = f"\n==================================================\n" \
|
||||
f"请求地址:{r_uri}\n" \
|
||||
f"请求方式:{r_method}\n" \
|
||||
f"请求头:{r_headers}\n" \
|
||||
f"请求内容:{r_body}\n" \
|
||||
f"请求curl命令:{r_curl}\n" \
|
||||
f"接口响应内容:{r_respone}\n" \
|
||||
f"接口响应头:{r_respone_headers}\n" \
|
||||
f"接口响应内容:{r_response}\n" \
|
||||
f"接口响应头:{r_response_headers}\n" \
|
||||
f"接口响应时长:{r_duration:.2f}秒\n" \
|
||||
f"HTTP状态码:{r_respone_status_code}\n" \
|
||||
f"HTTP状态码:{r_response_status_code}\n" \
|
||||
f"==================================================\n\n"
|
||||
|
||||
with allure.step("请求内容"):
|
||||
allure_attach_text("请求地址", f"{r_uri}")
|
||||
allure_attach_text("请求方式", f"{r_method}")
|
||||
allure_attach_text("请求头", f"{r_headers}")
|
||||
allure_attach_json("请求体", f"{r_body}")
|
||||
allure_attach_text("请求curl命令", f"{r_curl}")
|
||||
allure_attach_text("请求地址", r_uri)
|
||||
allure_attach_text("请求方式", r_method)
|
||||
allure_attach_json("请求头", r_headers)
|
||||
allure_attach_json("请求体", r_body)
|
||||
allure_attach_text("请求curl命令", r_curl)
|
||||
with allure.step("响应内容"):
|
||||
allure_attach_json("响应体", json.dumps(r_respone, ensure_ascii=False, indent=4))
|
||||
allure_attach_text("HTTP状态码", f"{r_respone_status_code}")
|
||||
allure_attach_text("响应头", f"{r_respone_headers}")
|
||||
allure_attach_json("响应体", r_response)
|
||||
allure_attach_text("HTTP状态码", f"{r_response_status_code}")
|
||||
allure_attach_json("响应头", r_response_headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
logger.info(_log_msg)
|
||||
|
@ -242,6 +242,11 @@ class BaseApi:
|
|||
headers.pop('User-Agent', None)
|
||||
return headers
|
||||
|
||||
@staticmethod
|
||||
def get_response_headers(response: requests.Response) -> CaseInsensitiveDict[str]:
|
||||
"""获取响应头"""
|
||||
return response.headers
|
||||
|
||||
@staticmethod
|
||||
def get_request_body(response: requests.Response) -> str | None:
|
||||
"""获取请求体内容"""
|
||||
|
|
|
@ -10,6 +10,7 @@ import platform
|
|||
|
||||
import allure
|
||||
import pytest
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from common.models import AllureAttachmentType
|
||||
from configs.paths_config import TEMP_DIR, ALLURE_REPORT_DIR
|
||||
|
@ -28,16 +29,40 @@ def allure_attach_text(name: str, body: str = None) -> None:
|
|||
:param body: 附件内容
|
||||
:return:
|
||||
"""
|
||||
# 确保 body 是字符串类型
|
||||
if body is None:
|
||||
body = "None" # 如果 body 为 None,设置为空字符串
|
||||
elif not isinstance(body, str):
|
||||
body = str(body) # 如果 body 不是字符串,强制转换为字符串
|
||||
allure.attach(body=body, name=name, attachment_type=allure.attachment_type.TEXT)
|
||||
|
||||
|
||||
def allure_attach_json(name: str, body: str = None) -> None:
|
||||
def allure_attach_json(name: str, body: str | dict | CaseInsensitiveDict | None = None) -> None:
|
||||
"""
|
||||
allure报告添加json格式附件
|
||||
:param name: 附件名称
|
||||
:param body: 附件内容
|
||||
:return:
|
||||
"""
|
||||
# 检查是否是 CaseInsensitiveDict 类型
|
||||
if isinstance(body, CaseInsensitiveDict):
|
||||
body = dict(body) # 将CaseInsensitiveDict 转换为普通字典,一般请求头或者响应头是这种格式
|
||||
# 尝试格式化 JSON
|
||||
try:
|
||||
if isinstance(body, dict):
|
||||
# 如果 body 是字典直接转换
|
||||
body = json.dumps(body, ensure_ascii=False, indent=4)
|
||||
elif body is None:
|
||||
# 如果 body 为 None,设置为空字符串
|
||||
body = "None"
|
||||
elif isinstance(body, str):
|
||||
# 如果是字符串,先尝试转成字典,再解析成JSON
|
||||
body = json.dumps(json.loads(body), indent=4, ensure_ascii=False)
|
||||
else:
|
||||
# 其他类型,直接传原始数据
|
||||
body = body
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
body = body # 解析失败,直接传原始数据
|
||||
allure.attach(body=body, name=name, attachment_type=allure.attachment_type.JSON)
|
||||
|
||||
|
||||
|
@ -71,7 +96,7 @@ def allure_attach_file(name: str, source: str):
|
|||
AllureAttachmentType.MP4: allure.attachment_type.MP4,
|
||||
AllureAttachmentType.OGG: allure.attachment_type.OGG,
|
||||
AllureAttachmentType.WEBM: allure.attachment_type.WEBM,
|
||||
AllureAttachmentType.PDF: allure.attachment_type.PDF, }
|
||||
AllureAttachmentType.PDF: allure.attachment_type.PDF}
|
||||
_attachment_type = attachment_type_mapping.get(getattr(AllureAttachmentType, _name.upper(), None), None)
|
||||
allure.attach.file(source=source, name=name,
|
||||
attachment_type=_attachment_type,
|
||||
|
|
Loading…
Reference in New Issue