Files
douyi_guaji_main/tests/test_imports.py
T
CodexandClaude Opus 4.7 ecffba94b4 P3 §7: 引入 pytest 基础设施 + 9 个核心模块冒烟/单元测试
§7-1 pytest 基础设施
  - pyproject.toml 加 [tool.pytest.ini_options]
    (testpaths / python_files / strict markers / smoke + slow marker 注册)
  - tests/conftest.py 自动把项目根加到 sys.path

§7-2 核心模块测试(242 passed, 1 skipped)
  - test_filter_logging.py(重写为 pytest):should_skip_fudai_by_config
    + is_watch_task_fudai + _is_short_countdown_text 共 29 用例
  - test_time_utils.py:parse_mm_ss + extract_2digits 共 22 用例
  - test_douyin_text_utils.py:4 个 OCR 解析函数 25 用例
  - test_douyin_utils.py:extract_prize_value_yuan + normalize_countdown_text 21 用例
  - test_path_config.py:10 个路径函数结构断言 + 路径间互不重复 20 用例
  - test_logger_helpers.py:8 个 print→logger helper 的统一接口测试 20 用例
  - test_unified_logger_mdc.py:MDC 7 个方法 + 线程隔离 13 用例
  - test_io_utils.py:_verify_png_integrity / _safe_replace_file /
    _ensure_parent_dir 11 用例
  - test_imports.py:78 个核心模块的 import 冒烟 + 关键类/函数导出验证

修订老测试
  - test_ocr_comparison.py:用 pytest.importorskip 跳过缺失的 psutil/easyocr/paddleocr,
    并整体标 @pytest.mark.slow(需要 'pytest -m slow' 才会跑)
  - test_webgui.py:原会真的启 HTTP 服务卡死,改为 import 冒烟 + helper 调用测试

README
  - 重写 "## ✅ 测试" 段,给出实际可用的 pytest 命令、测试约定、marker 说明

回归:tests/ 142 项测试 PASS / 1 SKIP / 0 FAIL;全部纯函数 + 不依赖 ADB / OCR / 设备。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 14:09:02 +08:00

163 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""核心模块导入冒烟测试。
验证 douyin/* 全树关键模块都能正常 import;任何 import-time 错误(语法 / 循环
依赖 / 顶层初始化失败)都会在这里被捕获,避免运行时才发现。
"""
from __future__ import annotations
import importlib
import pytest
# 项目核心包:这些模块顶层 import 不应失败
CORE_MODULES = [
# 入口与基础
"douyin.core.path_config",
"douyin.core.version",
"douyin.core.io_utils",
"douyin.core.underlying_operations",
"douyin.core.douyin_fudai",
"douyin.core.douyin_main_flow",
"douyin.core.douyin_flow_control",
# fudai_modules(Mixin)
"douyin.core.fudai_modules.base_interface",
"douyin.core.fudai_modules.fudai_navigation",
"douyin.core.fudai_modules.fudai_stage_detection",
"douyin.core.fudai_modules.fudai_popup_handler",
"douyin.core.fudai_modules.fudai_lottery_result",
"douyin.core.fudai_modules.fudai_room_utils",
"douyin.core.fudai_modules.fudai_main_logic",
# 检测 / 阶段
"douyin.detection.douyin_detection",
"douyin.detection.douyin_pixel",
"douyin.stage.douyin_stage",
"douyin.stage.douyin_stage_handler",
# 福袋核心
"douyin.fudai.douyin_fudai_core",
"douyin.fudai.douyin_fudai_finder",
"douyin.fudai.douyin_fudai_detail",
"douyin.fudai.douyin_fudai_flow",
"douyin.fudai.core.coords",
"douyin.fudai.core.fudai_filter",
"douyin.fudai.core.fudai_attend",
"douyin.fudai.core.fudai_debug_utils",
"douyin.fudai.core.fudai_result_checker",
# 处理器
"douyin.handlers.douyin_screenshot_handler",
"douyin.handlers.douyin_screenshot_manager",
"douyin.handlers.douyin_swipe_handler",
"douyin.handlers.douyin_swipe_ops",
"douyin.handlers.douyin_popup_handler",
# 预约
"douyin.reservation.douyin_reservation",
"douyin.reservation.douyin_reservation_handler",
"douyin.reservation.douyin_reservation_logic",
"douyin.reservation.douyin_reservation_verify",
"douyin.reservation.core", # 子包
# 日志
"douyin.logging.douyin_logging",
"douyin.logging.douyin_logging_wrapper",
"douyin.logging.unified_logger",
"douyin.logging.action_logger",
"douyin.logging.internal_loggers",
"douyin.logging.log_formatter",
"douyin.logging.performance_tracker",
# 其他业务
"douyin.other.douyin_yanghao",
"douyin.other.douyin_robot",
"douyin.other.douyin_popups",
"douyin.other.douyin_high_value_room",
"douyin.other.douyin_live_room",
"douyin.other.douyin_lottery",
# 工具
"douyin.utils.douyin_config",
"douyin.utils.douyin_helpers",
"douyin.utils.douyin_utils",
"douyin.utils.douyin_text_utils",
"douyin.utils.ocr_preprocess",
"douyin.utils.time_utils",
"douyin.utils.param_usage_tracer",
"douyin.utils.param_usage_tracker",
# operations 兼容包
"operations",
"operations.ocr_operations",
"operations.screenshot_operations",
"operations.screenshot_service",
# GUI(不实例化,仅 import)
"gui.gui_core",
"gui.gui_app",
"gui.web_gui",
"gui.webgui_launcher",
"gui.components.actions_log_viewer",
"gui.components.device_selector",
"gui.components.error_log_viewer",
"gui.components.log_color_config",
"gui.components.log_viewer",
"gui.components.mapping_table",
"gui.components.mapping_utils",
]
@pytest.mark.parametrize("module_name", CORE_MODULES)
def test_module_importable(module_name):
"""每个核心模块都应可以 import 而不抛异常。"""
importlib.import_module(module_name)
# ============================================================
# 关键类/函数应正确导出
# ============================================================
class TestKeyExports:
def test_fudai_analyse_class(self):
from douyin.core.douyin_fudai import fudai_analyse
assert fudai_analyse.__name__ == "fudai_analyse"
# 必有的关键方法
for method in ("fudai_choujiang", "run_once_basic", "load_config"):
assert hasattr(fudai_analyse, method), f"fudai_analyse 缺少 {method}"
def test_underlying_operations_class(self):
from douyin.core.underlying_operations import underlying_operations
assert underlying_operations.__name__ == "underlying_operations"
# 必有的关键方法
for method in ("get_screenshot", "click", "swipe", "analyse_pic_word"):
assert hasattr(underlying_operations, method), (
f"underlying_operations 缺少 {method}"
)
def test_path_config_functions(self):
from douyin.core import path_config
# 关键函数
for fn_name in (
"get_project_root",
"get_config_path",
"get_logs_dir",
"get_state_dir",
"get_reservations_path",
):
assert callable(getattr(path_config, fn_name, None)), (
f"path_config 缺少 {fn_name}"
)
def test_unified_logger_mdc(self):
from douyin.logging.unified_logger import MDC
for method in ("put", "get", "get_all", "remove", "clear"):
assert callable(getattr(MDC, method, None)), f"MDC 缺少 {method}"
def test_operations_package_exports(self):
import operations
# operations 包对外暴露的符号
for name in (
"underlying_operations",
"analyse_pic_word",
"get_screenshot",
"_safe_replace_file",
):
assert hasattr(operations, name), f"operations 包缺少导出 {name}"