2
我有这样一段代码Pytest:测试WebSocket连接
class RTMClient:
...
#not important code
...
async def connect(self, queue: asyncio.Queue):
"""
Connect to the websocket stream and iterate over the messages
dumping them in the Queue.
"""
ws_url=''#url aquisition amended to readability
try:
self._ws = await websockets.connect(ws_url)
while not self.is_closed:
msg = await self._ws.recv()
if msg is None:
break
await queue.put(json.loads(msg))
except asyncio.CancelledError:
pass
finally:
self._closed.set()
self._ws = None
我想要写的自动测试它。 我打算做:
- 猴补丁
websockets.connect
返回一个模拟连接 - 使模拟连接预设列表返回假消息
- 使模拟连接设置
is_closed
到True
- 断言websocket连接已关闭
- 断言所有预定义的消息都在队列中
我的问题:我如何模拟websockets.connection
实现步骤1-3? 我想到一个pytest夹具这样
from websockets import WebSocketClientProtocol()
@pytest.fixture
def patch_websockets_connect(monkeypatch):
async def mock_ws_connect(*args, **kwargs):
mock_connection = WebSocketClientProtocol()
mock_connection.is_closed = False
return mock_connection
monkeypatch.setattr('target_module.websockets.connect', mock_ws_connect)
但我不看我怎么就能返回消息的预定义列表这种方式,也必须有这样做的更好的方法。