Skip to content

Support for WebSockets #20

@gsmafra

Description

@gsmafra

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions