116 lines
3.5 KiB
Python
116 lines
3.5 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
|
@Time : 2025/7/30 10:58
|
||
|
@Auth : 九月的海
|
||
|
@File : conftest.py.py
|
||
|
@IDE : PyCharm
|
||
|
@Motto : Catch as catch can....
|
||
|
"""
|
||
|
# conftest.py
|
||
|
import pytest
|
||
|
import logging
|
||
|
import time
|
||
|
import json
|
||
|
from pathlib import Path
|
||
|
from api_auto_framework.core.request_wrapper import RequestWrapper
|
||
|
|
||
|
|
||
|
# ========================== 全局日志配置 ==========================
|
||
|
def configure_logger(name):
|
||
|
"""配置全局日志记录器"""
|
||
|
logger = logging.getLogger(name)
|
||
|
logger.setLevel(logging.INFO)
|
||
|
|
||
|
# 创建一个流处理器(控制台输出)
|
||
|
stream_handler = logging.StreamHandler()
|
||
|
|
||
|
# 创建格式器并添加到处理器
|
||
|
formatter = logging.Formatter(
|
||
|
'%(asctime)s - %(levelname)s - %(message)s',
|
||
|
datefmt='%Y-%m-%d %H:%M:%S'
|
||
|
)
|
||
|
stream_handler.setFormatter(formatter)
|
||
|
|
||
|
# 确保只添加一个处理器
|
||
|
if not logger.hasHandlers():
|
||
|
logger.addHandler(stream_handler)
|
||
|
|
||
|
return logger
|
||
|
|
||
|
|
||
|
# ========================== 公共函数定义 ==========================
|
||
|
def log_request_details(test_data, logger):
|
||
|
"""记录请求详细信息到日志"""
|
||
|
logger.info(f"测试名称: {test_data['test_name']}")
|
||
|
logger.info(f"请求方法: {test_data['method']}")
|
||
|
logger.info(f"请求URL: {test_data['url']}")
|
||
|
|
||
|
if "headers" in test_data and test_data["headers"]:
|
||
|
logger.info("请求头:")
|
||
|
for k, v in test_data["headers"].items():
|
||
|
logger.info(f" {k}: {v}")
|
||
|
|
||
|
if "cookies" in test_data and test_data["cookies"]:
|
||
|
logger.info("Cookies:")
|
||
|
for k, v in test_data["cookies"].items():
|
||
|
logger.info(f" {k}: {v}")
|
||
|
|
||
|
if "body" in test_data and test_data.get("body"):
|
||
|
logger.info("请求体:" + test_data.get("body"))
|
||
|
else:
|
||
|
logger.info(" 无请求体")
|
||
|
|
||
|
|
||
|
def log_response_details(response, logger):
|
||
|
"""记录响应详细信息到日志"""
|
||
|
result = RequestWrapper.formatted_response(response)
|
||
|
|
||
|
logger.info(f"响应状态码: {result['status_code']}")
|
||
|
body_data = result.get("body", {})
|
||
|
logger.info(f"响应数据: {body_data}")
|
||
|
return result
|
||
|
|
||
|
|
||
|
# ========================== Fixture定义 ==========================
|
||
|
@pytest.fixture(scope="session")
|
||
|
def api_logger(request):
|
||
|
"""为每个测试提供专用日志记录器"""
|
||
|
logger_name = f"test_{request.node.name}"
|
||
|
return configure_logger(logger_name)
|
||
|
|
||
|
|
||
|
@pytest.fixture(scope="session")
|
||
|
def log_assert(api_logger):
|
||
|
"""带有日志记录的断言函数"""
|
||
|
|
||
|
def wrapper(condition, success_msg, fail_msg, actual_value=None, expected_value=None):
|
||
|
if condition:
|
||
|
api_logger.info(f"✔ PASS: {success_msg}")
|
||
|
if actual_value is not None and expected_value is not None:
|
||
|
api_logger.info(f" 实际值: {actual_value} | 期望值: {expected_value}")
|
||
|
else:
|
||
|
api_logger.error(f"✖ FAIL: {fail_msg}")
|
||
|
if actual_value is not None and expected_value is not None:
|
||
|
api_logger.error(f" 实际值: {actual_value} | 期望值: {expected_value}")
|
||
|
|
||
|
# 触发实际断言
|
||
|
assert condition, fail_msg
|
||
|
|
||
|
return wrapper
|
||
|
|
||
|
|
||
|
@pytest.fixture(scope="session")
|
||
|
def setup_teardown(request, api_logger):
|
||
|
"""测试的前置和后置操作"""
|
||
|
# 前置操作
|
||
|
api_logger.info(f"======= 开始测试: {request.node.name} =======")
|
||
|
start_time = time.time()
|
||
|
|
||
|
yield
|
||
|
|
||
|
# 后置操作
|
||
|
end_time = time.time()
|
||
|
duration = end_time - start_time
|
||
|
api_logger.info(f"测试执行时长: {duration:.2f}秒")
|
||
|
api_logger.info(f"======= 结束测试: {request.node.name} =======")
|