refactor: 优化日志系统及增加定位转换器

- 更新 pytest.ini 统一配置日志格式和基础命令。
- 优化 main.py 增加测试后的日志自动备份与定期清理功能。
- 新增 finder.py 实现定位元素转换机制
This commit is contained in:
2026-01-21 15:29:52 +08:00
parent 5df8f686a6
commit e59ffa36d3
6 changed files with 256 additions and 20 deletions

17
main.py
View File

@@ -1,27 +1,38 @@
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
# 日志自动清理
def _clean_old_logs(backup_dir, keep_count=10):
files = sorted(Path(backup_dir).glob("pytest_*.log"), key=os.path.getmtime)
while len(files) > keep_count:
os.remove(files.pop(0))
file_to_remove = files.pop(0)
try:
os.remove(file_to_remove)
except OSError as e:
print(f"清理旧日志失败 {file_to_remove}: {e}")
def main():
try:
# 2. 执行 Pytest
# 建议保留你之前配置的 -s -v 等参数
exit_code = pytest.main(["test_cases", "-x", "-v", f"--alluredir={ALLURE_TEMP}"])
# 注意:-x 表示遇到错误立即停止,如果是全量回归建议去掉 -x
pytest.main(["test_cases", "-x", "-v", f"--alluredir={ALLURE_TEMP}"])
# 3. 生成报告
if ALLURE_TEMP.exists():
os.system(f'allure generate {ALLURE_TEMP} -o {REPORT_DIR} --clean')
# 使用 subprocess 替代 os.system更安全且跨平台兼容性更好
subprocess.run(f'allure generate {ALLURE_TEMP} -o {REPORT_DIR} --clean', shell=True, check=False)
except Exception as e:
print(f"自动化测试执行过程中发生异常: {e}")
finally:
# 4. 备份日志 (无论测试是否崩溃都执行)