Files
claude[bot] e757b904e8 为项目添加测试用例和测试配置
- 添加了完整的测试框架 (pytest)
- 为所有主要模块添加了单元测试
- 添加了测试配置文件 (pyproject.toml)
- 更新了 requirements.txt 以包含测试依赖
- 添加了测试指南 (tests/README.md)

这些测试涵盖了:
- utils.py 模块的所有功能
- config.py 模块的配置加载和环境变量处理
- ai_handler.py 模块的AI处理和通知功能
- prompt_utils.py 模块的prompt生成和配置更新
- scraper.py 模块的核心爬虫功能
- 各主要脚本的入口点测试

测试使用了pytest和unittest.mock来模拟外部依赖,确保测试的独立性和可重复性。

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: rainsfly <dingyufei615@users.noreply.github.com>
2025-08-09 02:55:50 +00:00
..

测试指南

本项目使用 pytest 作为测试框架。以下是运行测试的指南。

安装依赖

在运行测试之前,请确保已安装所有开发依赖项:

pip install -r requirements.txt

运行测试

运行所有测试

pytest

运行特定测试文件

pytest tests/test_utils.py

运行特定测试函数

pytest tests/test_utils.py::test_safe_get

生成覆盖率报告

coverage run -m pytest
coverage report
coverage html  # 生成 HTML 报告

测试文件结构

tests/
├── __init__.py
├── conftest.py          # 共享测试配置和 fixtures
├── test_ai_handler.py   # ai_handler.py 模块的测试
├── test_config.py       # config.py 模块的测试
├── test_login.py        # login.py 脚本的测试
├── test_prompt_generator.py  # prompt_generator.py 脚本的测试
├── test_prompt_utils.py # prompt_utils.py 模块的测试
├── test_scraper.py      # scraper.py 模块的测试
├── test_spider_v2.py    # spider_v2.py 脚本的测试
└── test_utils.py        # utils.py 模块的测试

编写新测试

  1. tests/ 目录中创建新的测试文件,文件名应以 test_ 开头
  2. 使用 test_ 前缀命名测试函数
  3. 为异步函数使用 @pytest.mark.asyncio 装饰器
  4. 使用 unittest.mock 模块模拟外部依赖和副作用

注意事项

  1. 一些测试可能需要复杂的模拟,特别是涉及 Playwright 的测试
  2. 某些测试可能需要实际的网络连接或外部服务
  3. 测试数据应尽可能使用模拟数据而不是真实数据