test_main.py 836 B

123456789101112131415161718192021222324252627282930
  1. # -*- coding: utf-8 -*-
  2. """
  3. 测试文件
  4. 注意:使用普通的 def 定义测试函数,不要使用 async def
  5. 执行命令: pytest tests/test.py
  6. """
  7. import pytest
  8. import websockets
  9. from fastapi.testclient import TestClient
  10. def test_check_health(test_client: TestClient):
  11. # 替换成你的实际 WS 地址(和前端一致)
  12. ws_url = "ws://127.0.0.1:8001/api/v1/ai/mcp/ws/chat"
  13. try:
  14. async with websockets.connect(ws_url) as websocket:
  15. print("✅ WS 连接成功!")
  16. # 接收后端推送的消息
  17. while True:
  18. msg = await websocket.recv()
  19. print("收到消息:", msg)
  20. except Exception as e:
  21. print("❌ WS 连接失败:", str(e))
  22. # 运行所有测试
  23. if __name__ == "__main__":
  24. pytest.main(["-v", "tests/test_main.py"])