I wonder how can I unittest on my flask websocket server. My application supports quite many interface on REST API(Flask-restful) and Web Socket(Flask-SocketIO). All of websocket "emit" are proceed on celery process. I have problem unit-testing those websocket logic.
Several guys said, First I have to seperate this rest-api + websocket application. Is it weird running whole application on a single server (logically)?
How to unit-testing websocket code?
Thank you
Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.
Flask, being a minimalist web framework, does not have WebSocket support built-in. The old Flask-Sockets extension, which has not been maintained in the last few years, provided support for this protocol.
Some people prefer separate apps for HTTP and WebSocket, but that is not something I would recommend blindly. There are cases in which it makes sense to do so, others that do not, specially if you are using Flask-SocketIO, which gives you a fairly powerful integration.
The Flask-SocketIO extension includes a test client, similar in concept to Flask's test client, but meant to be used with socket routes. Have you looked at that?
If you want an example application that uses Flask, Celery, Socket.IO and has a nice set of unit tests for everything, have a look at Flack: https://github.com/miguelgrinberg/flack. I gave a tutorial session at PyCon 2016 in which I cover this application in detail: https://www.youtube.com/watch?v=tdIIJuPh3SI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With