Creating test cases for WebSockets
WebSockets are components of our ch05-web and ch05-api projects. The applications use flask-sock to implement the WebSocket endpoints. So far, pytest can only provide the testing environment for WebSockets. However, it needs the websockets module to run, test, and assert the response of our WebSocket endpoints. So, install this module using the following pip command:
pip install websockets
There are three components that the websockets module can provide to pytest:
- The simulated route that will receive the message from the client
- The mock server
- The test function that will serve as the client
All these components must be async because running WebSockets requires the asyncio platform. So, also install the pytest-asyncio module to give asynchronous support to these three components:
Pip install pytest-asyncio
Then, start implementing the simulated or mocked view similar to the following implementation to receive and...