Take the tour ×

I'm working on a project that works with tornado's websocket functionality. I see a decent amount of documentation for working with asychronous code, but nothing on how this can be used to create unit tests that work with their WebSocket implementation.

Does tornado.testing provide the functionality to do this? If so, could someone provide a brief example of how to make it happen?

Thanks in advance.

share|improve this question
Yeah I saw that page. I could be wrong, but I believe the asynchronous test cases they document there are for regular HTTP requests, not WebSockets. –  Steve Gattuso May 31 '12 at 1:00
It sounds like you want to use a client to actually hit a test server, in which case I think the answer is no, it does not. tornado.testing doesn't include a websocket client. You could try something like Selenium. –  Cole Maclean Feb 8 at 18:34
add comment

2 Answers

up vote 1 down vote accepted

I've attempted to implement some unit tests on tornado.websocket.WebSocketHandler based handlers and got the following results:

First of all AsyncHTTPTestCase definitely has lack of web sockets support.

Still, one can use it at least to manage IOLoop and application stuff which is significant. Unfortunately, there is no WebSocket client provided with tornado, so here enter side-developed library.

Here is unit test on Web Sockets using Jef Balog's tornado websocket client with a small fix.

share|improve this answer
add comment

This answer (and the question) may be of interest, I use ws4py for the client and Tornado's AsyncTestCase which simplifies the whole thing.

share|improve this answer
I would love to see a full example of how to test async websockets in tornado. Do you have a blog post somewhere? –  Clint Aug 19 at 22:45
Hi, no I don't, we have just dropped ws4py for another python module (can't recall it's name right now). We have to do some adjustments due to this change. I will notify you (here) when we have something working again (in a week or two) –  Pedru Aug 23 at 13:37
OK, here it is. It uses sync web socket requests (for each request we receive a response), but you can make it asynchronous with some modifications. The web socket client library comes from here it's for python3, but there's a python2 version as well. b.t.w. there isn't any tornado component (I suppose your intention is to test a tornado server?) –  Pedru Sep 5 at 10:02
add comment

Your Answer


