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>
This commit is contained in:
1 parent
818c3139b1
commit
ecffba94b4
15 files changed
+1215
-74
No files matched your search
@@ -0,0 +1,119 @@
|
||||
"""douyin/core/path_config 的 pytest 单元测试。
|
||||
|
||||
只做"返回值类型正确 + 路径在项目根下 + 路径不重复"等结构性断言,
|
||||
不假设具体绝对路径(避免在不同机器上脆弱)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from douyin.core import path_config
|
||||
|
||||
|
||||
PROJECT_ROOT = path_config.get_project_root()
|
||||
|
||||
|
||||
def _is_under_root(p: str) -> bool:
|
||||
"""检查路径是否在项目根之内。"""
|
||||
return os.path.commonpath([os.path.abspath(p), PROJECT_ROOT]) == PROJECT_ROOT
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 项目根
|
||||
# ============================================================
|
||||
|
||||
class TestProjectRoot:
|
||||
def test_root_is_absolute(self):
|
||||
assert os.path.isabs(PROJECT_ROOT)
|
||||
|
||||
def test_root_exists(self):
|
||||
assert os.path.isdir(PROJECT_ROOT)
|
||||
|
||||
def test_root_contains_douyin_package(self):
|
||||
assert os.path.isdir(os.path.join(PROJECT_ROOT, "douyin"))
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 各路径函数:返回类型 + 在项目根下
|
||||
# ============================================================
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fn_name",
|
||||
[
|
||||
"get_project_root",
|
||||
"get_config_path",
|
||||
"get_logs_dir",
|
||||
"get_pic_dir",
|
||||
"get_screenshot_path",
|
||||
"get_state_dir",
|
||||
"get_reservations_path",
|
||||
"get_high_value_rooms_path",
|
||||
"get_assets_dir",
|
||||
"get_actions_log_path",
|
||||
],
|
||||
)
|
||||
def test_path_function_returns_path_under_root(fn_name):
|
||||
fn = getattr(path_config, fn_name)
|
||||
p = fn()
|
||||
assert isinstance(p, str), f"{fn_name} 应返回 str"
|
||||
assert p, f"{fn_name} 不应返回空字符串"
|
||||
assert _is_under_root(p), f"{fn_name}={p} 应位于项目根 {PROJECT_ROOT} 下"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 关键路径具体形态
|
||||
# ============================================================
|
||||
|
||||
class TestSpecificPaths:
|
||||
def test_config_path_endswith_config_json(self):
|
||||
assert path_config.get_config_path().endswith(
|
||||
os.path.join("config", "config.json")
|
||||
)
|
||||
|
||||
def test_reservations_path_in_douyin_core_state(self):
|
||||
# main 上重构后的"实际位置"
|
||||
assert path_config.get_reservations_path().endswith(
|
||||
os.path.join("douyin", "core", "state", "reservations.json")
|
||||
)
|
||||
|
||||
def test_state_dir_at_root_level(self):
|
||||
# state/ 是项目根下的目录(与 reservations.json 实际位置不同)
|
||||
state_dir = path_config.get_state_dir()
|
||||
assert state_dir.endswith(os.sep + "state") or state_dir.endswith("/state")
|
||||
|
||||
def test_high_value_rooms_path_in_state_dir(self):
|
||||
path = path_config.get_high_value_rooms_path()
|
||||
assert path.endswith(
|
||||
os.path.join("state", "high_value_rooms.json")
|
||||
)
|
||||
|
||||
def test_screenshot_path_in_pic_dir(self):
|
||||
path = path_config.get_screenshot_path()
|
||||
assert path.endswith(os.path.join("pic", "screenshot.png"))
|
||||
|
||||
def test_actions_log_in_logs_dir(self):
|
||||
path = path_config.get_actions_log_path()
|
||||
assert path.endswith(os.path.join("logs", "actions.log"))
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 路径间不应互相重复
|
||||
# ============================================================
|
||||
|
||||
def test_distinct_paths():
|
||||
paths = {
|
||||
path_config.get_config_path(),
|
||||
path_config.get_logs_dir(),
|
||||
path_config.get_pic_dir(),
|
||||
path_config.get_state_dir(),
|
||||
path_config.get_reservations_path(),
|
||||
path_config.get_high_value_rooms_path(),
|
||||
path_config.get_assets_dir(),
|
||||
path_config.get_screenshot_path(),
|
||||
path_config.get_actions_log_path(),
|
||||
}
|
||||
# set 长度 = 元素数量 → 全部路径互不相同
|
||||
expected = 9
|
||||
assert len(paths) == expected, f"应有 {expected} 条不同路径,实际 {len(paths)}"
|
||||
Reference in new issue
Block a user