如何使用tornado.testing创建WebSocket单元测试?

发布于 2025-01-06 22:07:44 字数 199 浏览 1 评论 0原文

我正在开发一个与龙卷风的 websocket 功能一起使用的项目。我看到了大量有关使用异步代码的文档,但没有任何关于如何使用它来创建与其 WebSocket 实现一起使用的单元测试的信息。

tornado.testing 是否提供执行此操作的功能?如果是这样,有人可以提供一个简短的例子来说明如何实现这一目标吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方觉久 2025-01-13 22:07:44

正如@Vladimir所说,您仍然可以使用AsyncHTTPTestCase来创建/管理测试Web服务器实例,但是您可以仍然以与正常HTTP请求相同的方式测试WebSockets - 没有任何语法糖可以帮助你。

Tornado 也有自己的 WebSocket 客户端,因此不需要(据我所知)使用第三方客户端 - 也许这是最近添加的。所以尝试这样的事情:

import tornado

class TestWebSockets(tornado.testing.AsyncHTTPTestCase):
    def get_app(self):
        # Required override for AsyncHTTPTestCase, sets up a dummy
        # webserver for this test.
        app = tornado.web.Application([
            (r'/path/to/websocket', MyWebSocketHandler)
        ])
        return app

    @tornado.testing.gen_test
    def test_websocket(self):
        # self.get_http_port() gives us the port of the running test server.
        ws_url = "ws://localhost:" + str(self.get_http_port()) + "/path/to/websocket"
        # We need ws_url so we can feed it into our WebSocket client.
        # ws_url will read (eg) "ws://localhost:56436/path/to/websocket

        ws_client = yield tornado.websocket.websocket_connect(ws_url)

        # Now we can run a test on the WebSocket.
        ws_client.write_message("Hi, I'm sending a message to the server.")
        response = yield ws_client.read_message()
        self.assertEqual(response, "Hi client! This is a response from the server.")
        # ...etc

希望无论如何这是一个好的起点。

As @Vladimir said, you can still use AsyncHTTPTestCase to create/manage the test webserver instance, but you can still test WebSockets in much the same way as you would normal HTTP requests - there's just no syntactic sugar to help you.

Tornado also has its own WebSocket client so there's no need (as far as I've seen) to use a third party client - perhaps it's a recent addition though. So try something like:

import tornado

class TestWebSockets(tornado.testing.AsyncHTTPTestCase):
    def get_app(self):
        # Required override for AsyncHTTPTestCase, sets up a dummy
        # webserver for this test.
        app = tornado.web.Application([
            (r'/path/to/websocket', MyWebSocketHandler)
        ])
        return app

    @tornado.testing.gen_test
    def test_websocket(self):
        # self.get_http_port() gives us the port of the running test server.
        ws_url = "ws://localhost:" + str(self.get_http_port()) + "/path/to/websocket"
        # We need ws_url so we can feed it into our WebSocket client.
        # ws_url will read (eg) "ws://localhost:56436/path/to/websocket

        ws_client = yield tornado.websocket.websocket_connect(ws_url)

        # Now we can run a test on the WebSocket.
        ws_client.write_message("Hi, I'm sending a message to the server.")
        response = yield ws_client.read_message()
        self.assertEqual(response, "Hi client! This is a response from the server.")
        # ...etc

Hopefully that's a good starting point anyway.

左秋 2025-01-13 22:07:44

我尝试在基于 tornado.websocket.WebSocketHandler 的处理程序上实现一些单元测试,并得到以下结果:

首先,AsyncHTTPTestCase 肯定缺乏 Web 套接字支持。

尽管如此,人们至少可以使用它来管理 IOLoop 和应用程序,这一点很重要。不幸的是,tornado 没有提供 WebSocket 客户端,因此这里输入侧面开发的库。

这是使用 Jef 的 Web Socket 单元测试 Balog 的 tornado websocket 客户端

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.

转身泪倾城 2025-01-13 22:07:44

这个答案(和问题)可能会感兴趣,我使用ws4py 用于客户端和 Tornado 的 AsyncTestCase 简化了整个过程。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文