100 lines
2.8 KiB
Python
100 lines
2.8 KiB
Python
"""
|
|||
|
|
API端到端测试
|
||
|
|
"""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
import requests
|
||
|
|
|
||
|
|
|
||
|
|
API_BASE = "http://localhost:7865"
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.e2e
|
||
|
|
class TestAPIEndpoints:
|
||
|
|
"""测试API端点"""
|
||
|
|
|
||
|
|
def test_api_info(self):
|
||
|
|
"""测试API信息端点"""
|
||
|
|
response = requests.get(f"{API_BASE}/api/info", timeout=10)
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert "job_id" in data
|
||
|
|
assert "working_dir" in data
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("query,expected_mode", [
|
||
|
|
("帮我分析拟南芥SPL基因家族", "analysis"),
|
||
|
|
("什么是SPL基因家族?", "faq"),
|
||
|
|
("推荐用什么参数做HMMER搜索?", "consultation"),
|
||
|
|
])
|
||
|
|
def test_chat_intent_recognition(self, query, expected_mode):
|
||
|
|
"""测试聊天意图识别"""
|
||
|
|
response = requests.post(
|
||
|
|
f"{API_BASE}/api/agent/chat",
|
||
|
|
json={
|
||
|
|
"message": query,
|
||
|
|
"session_id": "e2e_test_session"
|
||
|
|
},
|
||
|
|
timeout=30
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
|
||
|
|
# 检查响应结构
|
||
|
|
assert "intent" in data or "response" in data
|
||
|
|
|
||
|
|
def test_plan_endpoint(self):
|
||
|
|
"""测试规划端点"""
|
||
|
|
response = requests.post(
|
||
|
|
f"{API_BASE}/api/agent/plan",
|
||
|
|
json={
|
||
|
|
"message": "分析水稻SPL基因家族",
|
||
|
|
"gene_family": "SPL",
|
||
|
|
"species": "rice",
|
||
|
|
"session_id": "e2e_test_plan"
|
||
|
|
},
|
||
|
|
timeout=30
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
|
||
|
|
# 检查是否有规划结果
|
||
|
|
if "plan" in data:
|
||
|
|
plan = data["plan"]
|
||
|
|
assert "stages" in plan
|
||
|
|
|
||
|
|
def test_tasks_endpoint(self):
|
||
|
|
"""测试任务列表端点"""
|
||
|
|
response = requests.get(f"{API_BASE}/api/agent/tasks", timeout=10)
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
# 任务列表可以是空数组
|
||
|
|
data = response.json()
|
||
|
|
assert isinstance(data, list) or isinstance(data, dict)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.e2e
|
||
|
|
class TestServiceHealth:
|
||
|
|
"""测试服务健康状态"""
|
||
|
|
|
||
|
|
def test_service_running(self):
|
||
|
|
"""测试服务是否运行"""
|
||
|
|
try:
|
||
|
|
response = requests.get(f"{API_BASE}/api/info", timeout=5)
|
||
|
|
assert response.status_code == 200
|
||
|
|
except requests.exceptions.ConnectionError:
|
||
|
|
pytest.skip("服务未运行,跳过E2E测试")
|
||
|
|
|
||
|
|
def test_response_time(self):
|
||
|
|
"""测试响应时间"""
|
||
|
|
import time
|
||
|
|
|
||
|
|
start = time.time()
|
||
|
|
response = requests.get(f"{API_BASE}/api/info", timeout=10)
|
||
|
|
elapsed = time.time() - start
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
assert elapsed < 2.0 # 响应时间应小于2秒
|