mirror of
https://github.com/Usagi-org/ai-goofish-monitor.git
synced 2025-11-25 03:15:07 +08:00
- 添加了完整的测试框架 (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>
测试指南
本项目使用 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 模块的测试
编写新测试
- 在
tests/目录中创建新的测试文件,文件名应以test_开头 - 使用
test_前缀命名测试函数 - 为异步函数使用
@pytest.mark.asyncio装饰器 - 使用
unittest.mock模块模拟外部依赖和副作用
注意事项
- 一些测试可能需要复杂的模拟,特别是涉及 Playwright 的测试
- 某些测试可能需要实际的网络连接或外部服务
- 测试数据应尽可能使用模拟数据而不是真实数据