7

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.

  1. Several guys said, First I have to seperate this rest-api + websocket application. Is it weird running whole application on a single server (logically)?

  2. How to unit-testing websocket code?

Thank you

whiteUnicorn
  • 73
  • 1
  • 5

1 Answers1

10

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152