Pytest:测试 websocket 连接

Pytest: testing a websocket connection

我有这段代码

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

我想为它写一个自动化测试。 我打算做什么:

  1. Monkeypatch websockets.connect 到 return 模拟连接
  2. 建立模拟连接return 来自预定义列表的模拟消息
  3. 将模拟连接设置为 is_closedTrue
  4. 断言 websocket 连接已关闭
  5. 断言所有预定义消息都在队列中

我的问题:如何模拟 websockets.connection 以实现步骤 1-3? 我正在考虑像这样的 pytest fixture

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)

但我不明白我如何能够通过这种方式return 预定义的消息列表,并且必须有更好的方法来做到这一点。

这不是一个完整的答案,但也许会对你有所帮助。

我在测试 rabbitmq 消息发送时遇到了类似的问题。看起来这里最常见和最可靠的方法是创建 Connection、创建 EmmiterConsumer、将 Consumer 连接到假 Channel 并手动发送在测试中向该频道发送消息。

因此,您创建了所有对象并只是模拟了它们的响应。这是非常相似的示例(使用套接字而不使用 websockets,但可能对您仍然有用):