§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>
128 lines
3.8 KiB
Python
128 lines
3.8 KiB
Python
"""douyin/logging/unified_logger.MDC 的 pytest 单元测试。
|
||
|
||
MDC(Mapped Diagnostic Context)用线程本地存储跟踪 trace_id 等上下文。
|
||
覆盖 put / get / get_all / remove / clear / set_trace_id / get_trace_id 7 个方法。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import threading
|
||
|
||
import pytest
|
||
|
||
from douyin.logging.unified_logger import MDC
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def _clear_mdc_after_each_test():
|
||
"""每个测试结束都清空 MDC,避免相互污染。"""
|
||
yield
|
||
MDC.clear()
|
||
|
||
|
||
# ============================================================
|
||
# put / get / get_all / remove / clear
|
||
# ============================================================
|
||
|
||
class TestPutGet:
|
||
def test_put_and_get(self):
|
||
MDC.put("user_id", "alice")
|
||
assert MDC.get("user_id") == "alice"
|
||
|
||
def test_get_default_when_missing(self):
|
||
assert MDC.get("nonexistent") is None
|
||
assert MDC.get("nonexistent", "fallback") == "fallback"
|
||
|
||
def test_get_all_returns_copy(self):
|
||
MDC.put("k1", "v1")
|
||
MDC.put("k2", "v2")
|
||
all_ctx = MDC.get_all()
|
||
assert all_ctx == {"k1": "v1", "k2": "v2"}
|
||
# 修改返回的字典不应影响 MDC 内部状态
|
||
all_ctx["k3"] = "v3"
|
||
assert MDC.get("k3") is None
|
||
|
||
def test_overwrite_value(self):
|
||
MDC.put("k", "v1")
|
||
MDC.put("k", "v2")
|
||
assert MDC.get("k") == "v2"
|
||
|
||
|
||
class TestRemove:
|
||
def test_remove_existing_key(self):
|
||
MDC.put("k", "v")
|
||
MDC.remove("k")
|
||
assert MDC.get("k") is None
|
||
|
||
def test_remove_nonexistent_does_not_raise(self):
|
||
# 删不存在的 key 不应抛
|
||
MDC.remove("nonexistent")
|
||
|
||
|
||
class TestClear:
|
||
def test_clear_removes_everything(self):
|
||
MDC.put("k1", "v1")
|
||
MDC.put("k2", "v2")
|
||
MDC.clear()
|
||
assert MDC.get("k1") is None
|
||
assert MDC.get("k2") is None
|
||
assert MDC.get_all() == {}
|
||
|
||
def test_clear_on_empty_does_not_raise(self):
|
||
MDC.clear() # 空状态再 clear 也应不抛
|
||
|
||
|
||
# ============================================================
|
||
# trace_id
|
||
# ============================================================
|
||
|
||
class TestTraceId:
|
||
def test_set_explicit_trace_id(self):
|
||
result = MDC.set_trace_id("abc-123")
|
||
assert result == "abc-123"
|
||
assert MDC.get_trace_id() == "abc-123"
|
||
|
||
def test_set_auto_generates_trace_id(self):
|
||
result = MDC.set_trace_id()
|
||
# 自动生成的应是非空字符串
|
||
assert isinstance(result, str)
|
||
assert len(result) > 0
|
||
assert MDC.get_trace_id() == result
|
||
|
||
def test_get_trace_id_when_unset(self):
|
||
# 没设过的话,get 返回 None
|
||
assert MDC.get_trace_id() is None
|
||
|
||
def test_clear_removes_trace_id(self):
|
||
MDC.set_trace_id("abc")
|
||
MDC.clear()
|
||
assert MDC.get_trace_id() is None
|
||
|
||
|
||
# ============================================================
|
||
# 线程隔离:MDC 是线程本地存储
|
||
# ============================================================
|
||
|
||
class TestThreadLocal:
|
||
def test_mdc_is_thread_local(self):
|
||
"""主线程的 MDC 不应污染其他线程。"""
|
||
MDC.put("main_thread_key", "main_value")
|
||
|
||
results = {}
|
||
|
||
def worker():
|
||
# 子线程里看不到主线程的值
|
||
results["child_get"] = MDC.get("main_thread_key")
|
||
MDC.put("child_thread_key", "child_value")
|
||
results["child_self"] = MDC.get("child_thread_key")
|
||
|
||
t = threading.Thread(target=worker)
|
||
t.start()
|
||
t.join()
|
||
|
||
assert results["child_get"] is None, "子线程不应看到主线程的 MDC 值"
|
||
assert results["child_self"] == "child_value"
|
||
# 主线程也不应看到子线程的值
|
||
assert MDC.get("child_thread_key") is None
|
||
# 主线程自己的值还在
|
||
assert MDC.get("main_thread_key") == "main_value"
|