Files
douyin_guaji-main/tests/README.md
T
Your Name efaa3d3adc fix: Clean up blank lines and enhance readability across multiple files
- Removed excessive blank lines from various markdown and Python files to improve readability and maintainability.
- Ensured consistent formatting across documentation and code files, contributing to overall project clarity.
- Enhanced logging mechanisms for better consistency and debugging support.
2025-12-27 15:19:04 +08:00

3.4 KiB

测试套件文档

📋 目录结构

tests/
├── __init__.py
├── conftest.py              # pytest全局配置和fixtures
├── unit/                    # 单元测试
│   ├── __init__.py
│   ├── test_version.py
│   ├── test_douyin_utils.py
│   ├── test_douyin_text_utils.py
│   ├── test_path_config.py
│   ├── test_douyin_fudai_core.py
│   └── test_gui_core.py
├── integration/             # 集成测试
│   ├── __init__.py
│   ├── test_config_loading.py
│   └── test_fudai_analyse_init.py
├── fixtures/               # 测试数据
│   └── (测试图片、JSON文件等)
└── mocks/                  # Mock对象
    └── (可复用的Mock类)

🚀 快速开始

1. 安装测试依赖

pip install -r requirements-test.txt

2. 运行所有测试

# 使用pytest直接运行
pytest

# 或使用测试脚本
python run_tests.py

3. 运行特定类型的测试

# 只运行单元测试
python run_tests.py --unit

# 只运行集成测试
python run_tests.py --integration

# 运行特定标记的测试
pytest -m unit
pytest -m integration

4. 生成覆盖率报告

python run_tests.py --coverage
# 报告位置: htmlcov/index.html

5. 并行运行测试

python run_tests.py --parallel

📝 编写测试

单元测试示例

import pytest
from douyin_utils import normalize_countdown_text

class TestNormalizeCountdownText:
    def test_normalize_minutes_seconds(self):
        assert normalize_countdown_text("5分30秒") == "5:30"

使用Fixtures

def test_with_mock_analyser(mock_analyser):
    assert mock_analyser.device_id == "test_device_123"

标记测试

@pytest.mark.unit
def test_something():
    pass

@pytest.mark.integration
def test_integration():
    pass

@pytest.mark.requires_device
def test_with_device():
    pass

🎯 测试标记

  • @pytest.mark.unit - 单元测试
  • @pytest.mark.integration - 集成测试
  • @pytest.mark.slow - 慢速测试
  • @pytest.mark.requires_device - 需要真实设备
  • @pytest.mark.requires_gui - 需要GUI环境
  • @pytest.mark.requires_network - 需要网络连接
  • @pytest.mark.mock - 使用Mock的测试

📊 测试覆盖率目标

  • 核心模块: >= 80%
  • 工具模块: >= 90%
  • 整体: >= 70%

🔧 常用命令

# 运行特定文件
pytest tests/unit/test_version.py

# 运行特定测试类
pytest tests/unit/test_version.py::TestVersion

# 运行特定测试方法
pytest tests/unit/test_version.py::TestVersion::test_version_exists

# 显示详细输出
pytest -v

# 显示print输出
pytest -s

# 在第一个失败时停止
pytest -x

# 运行上次失败的测试
pytest --lf

📈 CI/CD集成

测试可以在CI/CD流程中自动运行:

# GitHub Actions示例
- name: Run tests
  run: |
    pip install -r requirements-test.txt
    pytest --cov=. --cov-report=xml

🐛 调试测试

# 使用pdb调试
pytest --pdb

# 使用ipdb调试(需要安装ipdb)
pytest --pdb --pdbcls=IPython.terminal.debugger:Pdb

📚 更多资源