refactor: 优化日志系统及自动化备份机制
- 替换 loguru 为原生 logging 库(与pytest兼容性更好)。 - 更新 pytest.ini 统一配置日志格式和基础命令。 - 优化 main.py 增加测试后的日志自动备份与定期清理功能。 - 新增 settings.py 实现配置解耦 - 更新 .gitignore
This commit is contained in:
72
conftest.py
Normal file
72
conftest.py
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
"""
|
||||
@author: CNWei,ChenWei
|
||||
@Software: PyCharm
|
||||
@contact: t6g888@163.com,chenwei@zygj.com
|
||||
@file: conftest
|
||||
@date: 2026/1/16 10:52
|
||||
@desc:
|
||||
"""
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
from core.run_appium import start_appium_service, stop_appium_service
|
||||
from core.driver import AppDriver
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def app_server():
|
||||
"""
|
||||
第一层:管理 Appium Server 进程。
|
||||
利用你原本 start_appium_service 里的 40 次轮询和 sys.exit(1) 逻辑。
|
||||
"""
|
||||
# 启动服务
|
||||
service = start_appium_service()
|
||||
yield service
|
||||
# 所有测试结束,清理进程
|
||||
stop_appium_service(service)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def driver(app_server):
|
||||
"""
|
||||
第二层:管理 WebDriver 会话。
|
||||
依赖 app_server,确保服务 Ready 后才创建连接。
|
||||
"""
|
||||
# 实例化你提供的类结构
|
||||
app_helper = AppDriver()
|
||||
|
||||
# 配置Android设备参数
|
||||
capabilities = dict(
|
||||
platformName='Android',
|
||||
automationName='uiautomator2',
|
||||
deviceName='Android',
|
||||
appPackage='com.android.settings',
|
||||
appActivity='.Settings'
|
||||
)
|
||||
|
||||
# 连接并获取原生 driver 实例
|
||||
app_helper.connect(platform="android", caps=capabilities)
|
||||
|
||||
yield app_helper
|
||||
|
||||
# 用例结束,只关 session,不关 server
|
||||
app_helper.quit()
|
||||
|
||||
|
||||
def pytest_exception_interact(node, call, report):
|
||||
"""
|
||||
当测试用例抛出异常(断言失败或代码报错)时,Pytest 会调用这个钩子。
|
||||
我们在这里手动把错误信息喂给 logging。
|
||||
"""
|
||||
# 获取名为 'Error' 的记录器,它会遵循 pytest.ini 中的 log_file 配置
|
||||
logger = logging.getLogger("Error")
|
||||
|
||||
if report.failed:
|
||||
# 获取详细的错误堆栈(包含 assert 的对比信息)
|
||||
exc_info = call.excinfo.getrepr(style='no-locals')
|
||||
|
||||
logger.error(f"TEST FAILED: {node.nodeid}")
|
||||
logger.error(f"详细错误信息如下:\n{exc_info}")
|
||||
40
core/settings.py
Normal file
40
core/settings.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
"""
|
||||
@author: CNWei,ChenWei
|
||||
@Software: PyCharm
|
||||
@contact: t6g888@163.com,chenwei@zygj.com
|
||||
@file: settings
|
||||
@date: 2026/1/19 16:54
|
||||
@desc:
|
||||
"""
|
||||
import os
|
||||
# core/settings.py
|
||||
from pathlib import Path
|
||||
|
||||
# 项目根目录 (core 的上一级)
|
||||
BASE_DIR = Path(__file__).parent.parent
|
||||
|
||||
# --- 目录配置 ---
|
||||
OUTPUT_DIR = BASE_DIR / "outputs"
|
||||
LOG_DIR = OUTPUT_DIR / "logs"
|
||||
LOG_BACKUP_DIR = LOG_DIR / "backups"
|
||||
ALLURE_TEMP = BASE_DIR / "temp"
|
||||
REPORT_DIR = BASE_DIR / "report"
|
||||
|
||||
# 确保必要的目录存在
|
||||
for folder in [LOG_DIR, LOG_BACKUP_DIR, ALLURE_TEMP]:
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# --- 文件路径 ---
|
||||
LOG_SOURCE = LOG_DIR / "pytest.log"
|
||||
|
||||
# --- 业务常量 (可选) ---
|
||||
IMPLICIT_WAIT = 10
|
||||
APPIUM_SERVER = "http://127.0.0.1:4723"
|
||||
# --- 核心配置 ---
|
||||
APPIUM_HOST = "127.0.0.1"
|
||||
APPIUM_PORT = 4723
|
||||
|
||||
|
||||
22
pytest.ini
Normal file
22
pytest.ini
Normal file
@@ -0,0 +1,22 @@
|
||||
[pytest]
|
||||
addopts = -q --show-capture=no
|
||||
;addopts = --tb=short
|
||||
# 1. 开启实时控制台日志
|
||||
log_cli = True
|
||||
log_cli_level = INFO
|
||||
log_cli_format = %(asctime)s %(levelname)-5s [%(name)s] - %(message)s
|
||||
log_cli_date_format = %H:%M:%S
|
||||
|
||||
# 2. 开启日志文件记录
|
||||
log_file = outputs/logs/pytest.log
|
||||
log_file_level = INFO
|
||||
log_file_format = %(asctime)s %(levelname)-5s [%(name)s] %(module)s.%(funcName)s:%(lineno)d - %(message)s
|
||||
log_file_date_format = %Y-%m-%d %H:%M:%S
|
||||
|
||||
# 3. 基础配置
|
||||
# 解决中文测试用例显示为乱码(Unicode)的问题
|
||||
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True
|
||||
|
||||
# 限制 Pytest 搜索范围,提升启动速度
|
||||
testpaths = test_cases
|
||||
python_files = test_*.py
|
||||
Reference in New Issue
Block a user