Files
douyin_guaji-main/tests/unit/test_douyin_utils.py
T
Your Name 539e580147 feat: Add coverage report generation and enhance testing framework
- Introduced a new `.coverage` file for tracking test coverage metrics.
- Added `pytest.ini` configuration for streamlined testing setup, including coverage reporting and logging options.
- Created `requirements-test.txt` to manage testing dependencies, ensuring a consistent testing environment.
- Implemented a `run_tests.py` script to facilitate running tests with various options, including unit and integration tests.
- Established a structured testing directory with unit and integration tests, enhancing code reliability and maintainability.
- Updated documentation to reflect new testing procedures and configurations.
2025-12-22 13:26:26 +08:00

71 lines
2.5 KiB
Python

"""
douyin_utils模块测试
"""
import pytest
from douyin_utils import normalize_countdown_text, parse_countdown_seconds
class TestNormalizeCountdownText:
"""倒计时文本标准化测试"""
def test_normalize_minutes_seconds(self):
"""测试分钟+秒格式"""
assert normalize_countdown_text("5分30秒") == "5:30"
assert normalize_countdown_text("10分5秒") == "10:05"
assert normalize_countdown_text("0分45秒") == "0:45"
def test_normalize_only_seconds(self):
"""测试只有秒的格式"""
assert normalize_countdown_text("30秒") == "0:30"
assert normalize_countdown_text("5秒") == "0:05"
def test_normalize_only_minutes(self):
"""测试只有分钟的格式"""
assert normalize_countdown_text("5分钟") == "5:00"
assert normalize_countdown_text("10分钟") == "10:00"
def test_normalize_colon_format(self):
"""测试已经是冒号格式的文本"""
assert normalize_countdown_text("5:30") == "5:30"
assert normalize_countdown_text("10:05") == "10:05"
def test_normalize_empty_string(self):
"""测试空字符串"""
assert normalize_countdown_text("") == ""
def test_normalize_invalid_format(self):
"""测试无效格式(应该返回原文本)"""
assert normalize_countdown_text("无效文本") == "无效文本"
assert normalize_countdown_text("abc") == "abc"
class TestParseCountdownSeconds:
"""倒计时秒数解析测试"""
def test_parse_minutes_seconds(self):
"""测试解析分钟+秒格式"""
assert parse_countdown_seconds("5:30") == 330
assert parse_countdown_seconds("10:05") == 605
assert parse_countdown_seconds("0:45") == 45
def test_parse_only_seconds(self):
"""测试解析只有秒的格式"""
assert parse_countdown_seconds("0:30") == 30
assert parse_countdown_seconds("0:05") == 5
def test_parse_only_minutes(self):
"""测试解析只有分钟的格式"""
assert parse_countdown_seconds("5:00") == 300
assert parse_countdown_seconds("10:00") == 600
def test_parse_invalid_format(self):
"""测试无效格式(应该返回0或抛出异常)"""
# 根据实际实现调整
try:
result = parse_countdown_seconds("无效")
assert result == 0 or result is None
except (ValueError, AttributeError):
pass # 如果抛出异常也是可以接受的