|
23 | 23 | 4. 环境配置 - 设置XPU相关环境变量 |
24 | 24 | """ |
25 | 25 |
|
| 26 | +import glob |
26 | 27 | import json |
27 | 28 | import os |
28 | 29 | import shutil |
|
31 | 32 |
|
32 | 33 | import pytest |
33 | 34 |
|
| 35 | +CASE_LOGS_DIR = os.path.join(os.getcwd(), "case_logs") |
| 36 | + |
34 | 37 |
|
35 | 38 | def get_xpu_id(): |
36 | 39 | """获取XPU_ID环境变量""" |
@@ -457,3 +460,42 @@ def setup_logprobs_zmq_env(): |
457 | 460 | os.environ[key] = value |
458 | 461 | print(f"设置环境变量: {key}={value}") |
459 | 462 | return original_values |
| 463 | + |
| 464 | + |
| 465 | +# ============ 日志归档 pytest hook ============ |
| 466 | + |
| 467 | + |
| 468 | +def _archive_case_logs(test_name): |
| 469 | + """ |
| 470 | + 将当前工作目录下所有 log 开头的文件夹和 server.log 复制到 case_logs/{test_name}/ 下 |
| 471 | + """ |
| 472 | + dest_dir = os.path.join(CASE_LOGS_DIR, test_name) |
| 473 | + os.makedirs(dest_dir, exist_ok=True) |
| 474 | + |
| 475 | + # 复制所有 log* 目录 |
| 476 | + for entry in glob.glob("log*"): |
| 477 | + if os.path.isdir(entry): |
| 478 | + shutil.copytree(entry, os.path.join(dest_dir, entry), dirs_exist_ok=True) |
| 479 | + elif os.path.isfile(entry): |
| 480 | + # 处理 server.log 等 log 开头的文件 |
| 481 | + shutil.copy2(entry, os.path.join(dest_dir, entry)) |
| 482 | + |
| 483 | + # 单独处理 server.log(不以 log 开头但也是关键日志) |
| 484 | + if os.path.exists("server.log") and not os.path.exists(os.path.join(dest_dir, "server.log")): |
| 485 | + shutil.copy2("server.log", os.path.join(dest_dir, "server.log")) |
| 486 | + |
| 487 | + |
| 488 | +@pytest.hookimpl(hookwrapper=True, trylast=True) |
| 489 | +def pytest_runtest_makereport(item, call): |
| 490 | + """每个测试阶段结束后归档日志(仅在 call 阶段后执行)""" |
| 491 | + outcome = yield |
| 492 | + report = outcome.get_result() |
| 493 | + |
| 494 | + if report.when == "call": |
| 495 | + # 使用测试文件名(不含 .py)作为归档目录名 |
| 496 | + test_file = os.path.basename(item.fspath) |
| 497 | + test_name = os.path.splitext(test_file)[0] |
| 498 | + try: |
| 499 | + _archive_case_logs(test_name) |
| 500 | + except Exception: |
| 501 | + pass |
0 commit comments