使用说明
主控程序 服务器
python中的天气MCP服务器
# Create a new directory for our project
uv init
# Create virtual environment and activate it
uv venv
source .venv/bin/activate
# Install dependencies
uv add "mcp[cli]" httpx
# Create our server file
touch weather.py
测试
该项目包括一个天气服务API的综合测试套件。测试使用pytest和pytest-asyncio来测试与美国国家气象局API交互的异步函数。
测试需求
- Python 3.12+
- pytest
- pytest异步
- pytest-cov 的
- httpx
安装开发依赖项
您可以使用以下方式安装所有开发依赖项:
# Using uv
uv pip install -e ".[dev]"
# Or using pip
pip install -e ".[dev]"
运行测试
要运行测试,请执行以下操作:
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run with code coverage
pytest --cov=weather
测试覆盖率
测试套件包括:
-
辅助功能的单元测试
format_alert格式化警报数据的功能- 错误处理
make_nws_request
-
API功能测试
get_alerts:测试成功检索、空结果和API失败get_forecast:测试API两个端点的成功预测检索和错误处理
-
模拟响应
- 所有HTTP请求都使用
httpx.AsyncMock - 具有不同API响应的多个测试场景
- 所有HTTP请求都使用
持续集成
在向天气服务添加新功能时,请在提交更改之前运行测试套件以确保所有测试通过。