""" 测试报告生成模块 本模块负责收集测试结果、生成测试报告。 包含测试用例记录、模块映射、报告合并、失败截图等功能。 """ import json import os from pathlib import Path import pytest from config.settings import config from tests.logger import get_logger, set_current_test # 测试日志记录器 _test_logger = get_logger("TEST") # 测试报告记录列表 _REPORT_RECORDS = [] # 模块名称映射(英文 -> 中文) _MODULE_NAME_MAP = { "data_generation": "数据生成", "data_set": "数据集", "data_space": "数据空间", "model_workshop": "模型工坊", "model_square": "模型广场", "device_manage": "设备管理", "material_library": "素材库", "lora_finetune": "LORA微调", "prompt_library": "系统提示词库", "monitor": "监控面板", "cloud_desktop": "云桌面", "dev_machine": "开发机", "mirror_center": "镜像中心", } def _parse_docstring(doc: str): """解析测试函数的 docstring,提取描述和预期结果 Args: doc: 测试函数的 docstring Returns: tuple: (描述, 预期结果) """ desc, expected = "", "" if not doc: return desc, expected for line in doc.strip().splitlines(): line = line.strip() if line.startswith("描述:"): desc = line[3:] elif line.startswith("预期:"): expected = line[3:] return desc, expected def _guess_module(nodeid: str) -> str: """根据 nodeid 猜测所属模块名称 Args: nodeid: pytest 节点的 nodeid Returns: str: 模块的中文名称,如果未找到则返回 nodeid 的第一部分 """ parts = nodeid.replace("\\", "/").split("/") for part in parts: if part in _MODULE_NAME_MAP: return _MODULE_NAME_MAP[part] return parts[0] if parts else "" @pytest.hookimpl(tryfirst=True) def pytest_runtest_setup(item): """测试开始前记录日志 在每个测试用例开始执行前调用,设置当前测试名称并记录开始信息。 同时从测试函数的 docstring 中提取描述信息。 Args: item: pytest 测试项对象 """ set_current_test(item.name) # 获取测试描述 doc = getattr(item.obj, "__doc__", None) or "" desc = "" for line in doc.strip().splitlines(): line = line.strip() if line.startswith("描述:"): desc = line[3:] break _test_logger.info(f"========== 测试开始: {item.name} | {desc} ==========") @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): """测试结果报告钩子 在测试执行后收集测试结果信息,包括: - 测试通过/失败/跳过状态 - 测试耗时 - 失败时的错误详情 - 测试描述和预期结果 Args: item: pytest 测试项对象 call: pytest 调用上下文 Returns: 测试结果报告对象 """ outcome = yield report = outcome.get_result() # 测试失败时截图 screenshot_path = None if report.when == "call" and report.failed: page = item.funcargs.get("page") if page: screenshot_name = f"{item.name}_failure" screenshot_path = f"{config.SCREENSHOT_DIR}/{screenshot_name}.png" try: page.screenshot(path=screenshot_path) except Exception: screenshot_path = None # 记录 call 阶段(passed/failed)和 setup 阶段(skipped) should_record = report.when == "call" or (report.when == "setup" and report.skipped) if should_record: doc = getattr(item.obj, "__doc__", None) or "" desc, expected = _parse_docstring(doc) nodeid = report.nodeid parts = nodeid.split("::") class_name = parts[-2] if len(parts) >= 3 else "" func_name = parts[-1] if parts else nodeid if report.passed: result = "通过" elif report.skipped: result = "跳过" else: result = "失败" error_detail = "" assertion_msg = "" failure_reason = "" if report.failed and report.longrepr: error_detail = str(report.longrepr) lines = error_detail.split("\n") for line in lines: stripped = line.strip() if stripped.startswith("AssertionError"): failure_reason = stripped break elif stripped.startswith("E ") and "AssertionError" in stripped: failure_reason = stripped elif stripped.startswith("assert "): assertion_msg = stripped if not failure_reason: for line in lines: stripped = line.strip() if stripped and not stripped.startswith("_"): failure_reason = stripped[:200] break _REPORT_RECORDS.append( { "module": _guess_module(nodeid), "class": class_name, "function": func_name, "nodeid": nodeid, "description": desc, "expected": expected, "result": result, "duration": round(report.duration, 2), "error": error_detail, "assertion": assertion_msg, "reason": failure_reason, "screenshot": screenshot_path, } ) # 测试结束后记录结果 if call.when == "call": if report.passed: _test_logger.info( f"========== 测试通过: {item.name} | 耗时 {report.duration:.2f}s ==========" ) elif report.failed: _test_logger.error( f"========== 测试失败: {item.name} | 耗时 {report.duration:.2f}s ==========" ) set_current_test("") def pytest_sessionfinish(session, exitstatus): """测试会话结束钩子 在所有测试执行完成后合并测试报告: - xdist 并行模式:每个 worker 写入临时文件,主进程合并 - 非并行模式:直接写入最终报告文件 Args: session: pytest 会话对象 exitstatus: 测试退出状态码 """ reports_dir = Path(__file__).parent.parent / "reports" reports_dir.mkdir(exist_ok=True) # xdist 并行时每个 worker 写临时文件,主进程合并 worker_id = os.environ.get("PYTEST_XDIST_WORKER", "") if worker_id: # worker 进程:写临时文件(即使没有记录也写空列表) tmp_file = reports_dir / f"report_data_{worker_id}.json" with open(tmp_file, "w", encoding="utf-8") as f: json.dump(_REPORT_RECORDS, f, ensure_ascii=False, indent=2) else: # 主进程(或非 xdist 模式):合并所有 worker 临时文件 all_records = list(_REPORT_RECORDS) for tmp_file in sorted(reports_dir.glob("report_data_gw*.json")): try: with open(tmp_file, encoding="utf-8") as f: all_records.extend(json.load(f)) tmp_file.unlink() # 合并后删除临时文件 except Exception: pass if not all_records: return data_file = reports_dir / "report_data.json" with open(data_file, "w", encoding="utf-8") as f: json.dump(all_records, f, ensure_ascii=False, indent=2)