§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>
70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
"""douyin/utils/time_utils 的 pytest 单元测试。
|
||
|
||
覆盖 parse_mm_ss(mm:ss 解析)和 extract_2digits(数字提取)两个纯函数。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import pytest
|
||
|
||
from douyin.utils.time_utils import extract_2digits, parse_mm_ss
|
||
|
||
|
||
# ============================================================
|
||
# parse_mm_ss
|
||
# ============================================================
|
||
|
||
class TestParseMmSs:
|
||
@pytest.mark.parametrize(
|
||
"text,expected",
|
||
[
|
||
("11:37", (11, 37)),
|
||
("00:00", (0, 0)),
|
||
("5:30", (5, 30)),
|
||
("12:59", (12, 59)),
|
||
# 中文冒号
|
||
("11:37".replace(":", ":"), (11, 37)),
|
||
("5:30后开奖".replace(":", ":"), (5, 30)),
|
||
# 嵌在文本里
|
||
("剩余 03:25 开奖", (3, 25)),
|
||
],
|
||
)
|
||
def test_valid(self, text, expected):
|
||
assert parse_mm_ss(text) == expected
|
||
|
||
@pytest.mark.parametrize(
|
||
"text",
|
||
[
|
||
"",
|
||
None,
|
||
"abc",
|
||
"12分钟", # 没冒号
|
||
"12",
|
||
"::",
|
||
],
|
||
)
|
||
def test_invalid(self, text):
|
||
assert parse_mm_ss(text) is None
|
||
|
||
|
||
# ============================================================
|
||
# extract_2digits
|
||
# ============================================================
|
||
|
||
class TestExtract2Digits:
|
||
@pytest.mark.parametrize(
|
||
"text,expected",
|
||
[
|
||
("12", "12"),
|
||
("123", "12"), # 取前 2 位
|
||
("9", "09"), # 单位补 0
|
||
("a3b", "03"), # 过滤非数字
|
||
("10秒", "10"),
|
||
("剩余3", "03"),
|
||
("", ""), # 无数字 → 空
|
||
("abc", ""),
|
||
(None, ""),
|
||
],
|
||
)
|
||
def test_extract(self, text, expected):
|
||
assert extract_2digits(text) == expected
|