eugeniy / pytest-tornado

A py.test plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.
Apache License 2.0
121 stars 34 forks source link

Support for WebSockets #20

Open gsmafra opened 7 years ago

gsmafra commented 7 years ago

Is there, even if unintentionally, support for testing a Tornado WebSocket server with pytest-tornado? If yes, how would you go to make a basic example, similar to the one shown for the HTTP server?

This is what I tried to do but it gets stuck waiting for the websocket_connect to yield something

import pytest
import tornado.websocket

class MainHandler(tornado.websocket.WebSocketHandler):

    def open(self):
        print('Websocket connection open')

    def on_message(self, message):
        self.write_message(message)
        print('Websocket message received: %s' % message)

    def on_close(self):
        print('Websocket connection closed')

application = tornado.web.Application([
    (r"/", MainHandler),
])

@pytest.fixture
def app():
    return application

@pytest.mark.gen_test
def test_hello_world(http_port):
    base_url = f"ws://localhost:{http_port}/"
    client = yield tornado.websocket.websocket_connect(base_url)
    client.write_message('message')
    response = yield client.read_message()
    assert response is not None
naught101 commented 4 years ago

I'm also interested in this, but can't get it working due to https://github.com/eugeniy/pytest-tornado/issues/56, and I'm having trouble figuring out how to turn it into a non-gen-test test. Any ideas welcome.

Pytest updates also mean http_port has to change to http_server_port, I think.

Is the reason this test is not working perhaps because the app is created, but never actually started?