§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>
132 lines
3.9 KiB
Python
132 lines
3.9 KiB
Python
"""douyin/utils/douyin_text_utils 的 pytest 单元测试。
|
|
|
|
覆盖福袋内容/倒计时/价值/参与人数 4 个 OCR 文本解析函数。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from douyin.utils.douyin_text_utils import (
|
|
_safe_str,
|
|
extract_fudai_content,
|
|
extract_fudai_countdown,
|
|
extract_fudai_value,
|
|
extract_participants_count,
|
|
)
|
|
|
|
|
|
# ============================================================
|
|
# _safe_str
|
|
# ============================================================
|
|
|
|
class TestSafeStr:
|
|
@pytest.mark.parametrize(
|
|
"value,expected",
|
|
[
|
|
(None, ""),
|
|
("", ""),
|
|
("abc", "abc"),
|
|
(123, "123"),
|
|
(0, "0"),
|
|
],
|
|
)
|
|
def test_safe_str(self, value, expected):
|
|
assert _safe_str(value) == expected
|
|
|
|
|
|
# ============================================================
|
|
# extract_fudai_content
|
|
# ============================================================
|
|
|
|
class TestExtractFudaiContent:
|
|
@pytest.mark.parametrize(
|
|
"text,expected_substr",
|
|
[
|
|
("福袋内容:现金红包 100 元", "现金红包 100 元"),
|
|
("奖品内容:iPhone 15", "iPhone 15"),
|
|
("内容:手机一台", "手机一台"),
|
|
# 全/半角冒号都支持
|
|
("福袋内容: 现金", "现金"),
|
|
],
|
|
)
|
|
def test_with_prefix(self, text, expected_substr):
|
|
result = extract_fudai_content(text)
|
|
assert expected_substr in result
|
|
|
|
def test_no_prefix_returns_original(self):
|
|
text = "随便一段没有前缀的描述"
|
|
# 没有前缀时返回原文(去首尾空白),不抛异常
|
|
assert extract_fudai_content(text) == text.strip()
|
|
|
|
@pytest.mark.parametrize("value", ["", None])
|
|
def test_empty(self, value):
|
|
assert extract_fudai_content(value) == ""
|
|
|
|
|
|
# ============================================================
|
|
# extract_fudai_countdown
|
|
# ============================================================
|
|
|
|
class TestExtractFudaiCountdown:
|
|
@pytest.mark.parametrize(
|
|
"value",
|
|
["", None],
|
|
)
|
|
def test_empty_returns_empty_string(self, value):
|
|
assert extract_fudai_countdown(value) == ""
|
|
|
|
def test_returns_string_type(self):
|
|
# 复杂文本走 normalize_countdown_text,结果应总是 str
|
|
result = extract_fudai_countdown("剩余 05:30 开奖")
|
|
assert isinstance(result, str)
|
|
|
|
|
|
# ============================================================
|
|
# extract_fudai_value
|
|
# ============================================================
|
|
|
|
class TestExtractFudaiValue:
|
|
@pytest.mark.parametrize(
|
|
"value",
|
|
["", None],
|
|
)
|
|
def test_empty_returns_zero(self, value):
|
|
assert extract_fudai_value(value) == 0.0
|
|
|
|
def test_returns_float(self):
|
|
# 不论输入是什么,返回值必须是 float
|
|
result = extract_fudai_value("价值 100 元")
|
|
assert isinstance(result, float)
|
|
# 至少应该解析出一个非负数
|
|
assert result >= 0.0
|
|
|
|
|
|
# ============================================================
|
|
# extract_participants_count
|
|
# ============================================================
|
|
|
|
class TestExtractParticipantsCount:
|
|
@pytest.mark.parametrize(
|
|
"value",
|
|
["", None],
|
|
)
|
|
def test_empty_returns_zero(self, value):
|
|
assert extract_participants_count(value) == 0
|
|
|
|
@pytest.mark.parametrize(
|
|
"text,expected",
|
|
[
|
|
("已1173人已参与", 1173),
|
|
("已经500人参与", 500),
|
|
("1173人已参与", 1173),
|
|
("1000人参与", 1000),
|
|
],
|
|
)
|
|
def test_extract_pattern(self, text, expected):
|
|
assert extract_participants_count(text) == expected
|
|
|
|
def test_returns_int(self):
|
|
result = extract_participants_count("100 人")
|
|
assert isinstance(result, int)
|
|
assert result >= 0
|