测试部署
测试规范
单元测试
测试用例编写
# tests/test_dialogue.py
import pytest
from dialogue_system import DialogueSystem
def test_dialogue_initialization():
system = DialogueSystem()
assert system.history == []
assert system.current_character is None
@pytest.mark.asyncio
async def test_message_processing():
system = DialogueSystem()
system.current_character = Character("测试角色", {})
response = await system.process_message("你好")
assert isinstance(response, str)
assert len(system.history) == 2
测试覆盖率检查
# 运行测试并生成覆盖率报告
pytest --cov=. tests/
coverage report
coverage html # 生成HTML报告
集成测试
API测试
# tests/test_api.py
async def test_complete_dialogue_flow():
"""测试完整对话流程"""
client = TestClient(app)
# 1. 选择角色
response = client.post("/api/character/select", json={"name": "测试角色"})
assert response.status_code == 200
# 2. 发送消息
response = client.post("/api/chat", json={"message": "你好"})
assert response.status_code == 200
assert "content" in response.json()
性能测试
# tests/test_performance.py
def test_response_time():
"""测试响应时间"""
start_time = time.time()
response = system.process_message("测试消息")
end_time = time.time()
assert end_time - start_time < 2.0 # 响应时间应小于2秒
部署流程
环境准备
生产环境配置
# 创建生产环境配置文件
cat > .env.production << EOL
HUGGINGFACE_TOKEN=your_production_token
MODEL_PATH=production_model_path
LOG_LEVEL=WARNING
ENABLE_MONITORING=true
EOL
依赖检查
# 检查依赖更新
pip list --outdated
pip install -U -r requirements.txt
# 生成依赖锁文件
pip freeze > requirements.lock
部署步骤
- Hugging Face
- Docker部署
# 1. 提交代码
git add .
git commit -m "Ready for deployment"
git push
# 2. 检查部署状态
huggingface-cli space status role-play-chat
# Dockerfile
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
监控维护
系统监控
日志配置
# utils/logging.py
import logging
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('app.log'),
logging.StreamHandler()
]
)