refactor(core/utils): 重构装饰器架构与日志追踪逻辑
- 优化 将 trace_step 移至 decorators.py,并引入 ContextVar 实现日志层级缩进。 - 新增 完善 StepTracer 和 action_screenshot 的 Docstrings,明确参数含义。 - 移除 清理了 logger.py - 优化 main.py 中重复的目录创建逻辑及旧版冗余注释。 - 规范 修正函数命名,提升代码在 BasePage 方案下的复用性。
This commit is contained in:
69
main.py
69
main.py
@@ -9,53 +9,80 @@
|
||||
@date: 2026/1/13 16:54
|
||||
@desc:
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from core.settings import LOG_SOURCE, LOG_BACKUP_DIR, ALLURE_TEMP, REPORT_DIR
|
||||
from core.settings import LOG_SOURCE, LOG_BACKUP_DIR, ALLURE_TEMP
|
||||
from utils.dirs_manager import ensure_dirs_ok
|
||||
from utils.report_handler import generate_allure_report
|
||||
|
||||
|
||||
# netstat -ano | findstr :4723
|
||||
# taskkill /PID 12345 /F
|
||||
# 日志自动清理
|
||||
|
||||
def _archive_logs():
|
||||
"""
|
||||
在测试开始前,归档上一次运行的日志文件。
|
||||
此时没有任何句柄占用,move 操作是 100% 安全的。
|
||||
"""
|
||||
# 4. 备份日志 (无论测试是否崩溃都执行)
|
||||
if LOG_SOURCE.exists() and LOG_SOURCE.stat().st_size > 0:
|
||||
now = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
backup_path = LOG_BACKUP_DIR / f"pytest_{now}.log"
|
||||
try:
|
||||
# 移动并重命名
|
||||
shutil.move(str(LOG_SOURCE), str(backup_path))
|
||||
print(f"已自动归档上次运行的日志: {backup_path}")
|
||||
# shutil.copy2(LOG_SOURCE, backup_path)
|
||||
# print(f"日志已备份至: {backup_path}")
|
||||
_clean_old_logs(LOG_BACKUP_DIR)
|
||||
except Exception as e:
|
||||
print(f"归档旧日志失败 (可能被外部编辑器打开): {e}")
|
||||
else:
|
||||
print("未找到原始日志文件,跳过备份。")
|
||||
|
||||
# 日志清理
|
||||
def _clean_old_logs(backup_dir, keep_count=10):
|
||||
files = sorted(Path(backup_dir).glob("pytest_*.log"), key=os.path.getmtime)
|
||||
files = sorted(Path(backup_dir).glob("pytest_*.log"), key=lambda p: p.stat().st_mtime)
|
||||
while len(files) > keep_count:
|
||||
file_to_remove = files.pop(0)
|
||||
try:
|
||||
os.remove(file_to_remove)
|
||||
file_to_remove.unlink(missing_ok=True)
|
||||
except OSError as e:
|
||||
print(f"清理旧日志失败 {file_to_remove}: {e}")
|
||||
|
||||
def _clean_temp_dirs():
|
||||
"""
|
||||
可选:如果你想在测试前清理掉旧的临时文件
|
||||
"""
|
||||
if ALLURE_TEMP.exists():
|
||||
shutil.rmtree(ALLURE_TEMP)
|
||||
# 加上 ignore_errors 是为了防止文件被占用导致整个测试无法启动
|
||||
shutil.rmtree(ALLURE_TEMP, ignore_errors=True)
|
||||
ALLURE_TEMP.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def main():
|
||||
try:
|
||||
# 2. 执行 Pytest
|
||||
# 建议保留你之前配置的 -s -v 等参数
|
||||
# 1. 创建目录
|
||||
ensure_dirs_ok()
|
||||
|
||||
# 2. 处理日志
|
||||
_archive_logs()
|
||||
|
||||
# 3. 执行 Pytest
|
||||
# 注意:-x 表示遇到错误立即停止,如果是全量回归建议去掉 -x
|
||||
pytest.main(["test_cases", "-x", "-v", f"--alluredir={ALLURE_TEMP}"])
|
||||
|
||||
# 3. 生成报告
|
||||
if ALLURE_TEMP.exists():
|
||||
# 使用 subprocess 替代 os.system,更安全且跨平台兼容性更好
|
||||
subprocess.run(f'allure generate {ALLURE_TEMP} -o {REPORT_DIR} --clean', shell=True, check=False)
|
||||
# 4. 生成报告
|
||||
generate_allure_report()
|
||||
except Exception as e:
|
||||
print(f"自动化测试执行过程中发生异常: {e}")
|
||||
|
||||
finally:
|
||||
# 4. 备份日志 (无论测试是否崩溃都执行)
|
||||
if LOG_SOURCE.exists():
|
||||
now = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
backup_path = LOG_BACKUP_DIR / f"pytest_{now}.log"
|
||||
shutil.copy2(LOG_SOURCE, backup_path)
|
||||
print(f"日志已备份至: {backup_path}")
|
||||
_clean_old_logs(LOG_BACKUP_DIR)
|
||||
else:
|
||||
print("未找到原始日志文件,跳过备份。")
|
||||
print("Time-of-check to Time-of-use")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user