refactor(core/utils): 重构装饰器架构与日志追踪逻辑

- 优化 将 trace_step 移至 decorators.py,并引入 ContextVar 实现日志层级缩进。
- 新增 完善 StepTracer 和 action_screenshot 的 Docstrings,明确参数含义。
- 移除 清理了 logger.py
- 优化 main.py 中重复的目录创建逻辑及旧版冗余注释。
- 规范 修正函数命名,提升代码在 BasePage 方案下的复用性。
This commit is contained in:
2026-02-03 17:46:48 +08:00
parent 798b5a8142
commit 483a31793d
9 changed files with 299 additions and 163 deletions

View File

@@ -1,52 +0,0 @@
#!/usr/bin/env python
# coding=utf-8
"""
@author: CNWei,ChenWei
@Software: PyCharm
@contact: t6g888@163.com
@file: conftest
@date: 2026/1/19 14:08
@desc:
"""
import pytest
import allure
from pathlib import Path
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""
本钩子函数会在每个测试阶段setup, call, teardown执行后被调用。
item: 测试用例对象
call: 测试执行阶段的信息
"""
# 1. 先执行常规的用例报告生成逻辑
outcome = yield
report = outcome.get_result()
# 2. 我们只关注测试执行阶段 ("call")
# 如果该阶段失败了failed则触发截图
if report.when == "call" and report.failed:
# 3. 从测试用例中获取 driver 实例
# 假设你在 fixture 中注入的参数名为 'driver'
driver_instance = item.funcargs.get("driver")
if driver_instance:
try:
# 4. 调用你在 CoreDriver 中实现的底层截图方法
# 这里的 name 我们可以动态取测试用例的名字
case_name = item.name
file_path = driver_instance.full_screen_screenshot(name=f"CRASH_{case_name}")
# 5. 如果路径存在,将其关联到 Allure 报告
if file_path:
p = Path(file_path)
if p.exists():
allure.attach.file(
source=p,
name="【故障现场自动截图】",
attachment_type=allure.attachment_type.PNG
)
except Exception as e:
print(f"故障自动截图执行失败: {e}")

View File

@@ -11,20 +11,23 @@
"""
import logging
from utils.logger import trace_step
import allure
from utils.decorators import step_trace
logger = logging.getLogger(__name__)
@trace_step("验证失败",)
@allure.epic("中银国际移动端重构项目")
@allure.feature("登认证模块")
@step_trace("验证失败",)
def test_settings_page_display(driver):
"""
测试设置页面是否成功加载
"""
# 此时 driver 已经通过 fixture 完成了初始化
current_act = driver.driver.current_activity
current_act = driver.current_activity
logger.info(f"捕获到当前 Activity: {current_act}")
assert ".unihome.UniHomeLauncher" in current_act
assert ".app.main.launcher.LauncherActivity" in current_act
def test_wifi_entry_exists(driver):
@@ -34,4 +37,4 @@ def test_wifi_entry_exists(driver):
# 这里的 driver 就是 appium.webdriver.Remote 实例
# 假设我们要查找“网络”相关的 ID
# el = driver.find_element(by='id', value='android:id/title')
assert driver.driver.session_id is not None
assert driver.session_id is not None