在网络线程上使用 pyunit

发布于 2024-07-28 22:18:45 字数 274 浏览 8 评论 0原文

我的任务是为一套用 python 编写的网络软件编写单元测试。 为消息生成器和其他静态方法编写单元非常简单,但在为网络循环线程编写测试时我遇到了困难。

例如:它连接到的服务器可以位于任何端口上,我希望能够测试连接到多个端口(按顺序,而不是并行)的能力,而无需实际运行多个服务器。 解决这个问题的好方法是什么? 也许将服务器构建和销毁作为测试的一部分? 有件事告诉我一定有一个更简单的答案却让我无法回答。

我不得不想象有一些方法可以对网络线程进行单元测试,但我似乎找不到任何方法。

I am tasked with writing unit tests for a suite of networked software written in python. Writing units for message builders and other static methods is very simple, but I've hit a wall when it comes to writing a tests for network looped threads.

For example: The server it connects to could be on any port, and I want to be able to test the ability to connect to numerous ports (in sequence, not parallel) without actually having to run numerous servers. What is a good way to approach this? Perhaps make server construction and destruction part of the test? Something tells me there must a simpler answer that evades me.

I have to imagine there are methods for unit testing networked threads, but I can't seem to find any.

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

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

发布评论

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

评论(4

电影里的梦 2024-08-04 22:18:45

我会尝试在您现有的代码中引入一个工厂,旨在创建套接字对象。 然后在模拟工厂中的测试通过中,创建模拟套接字,假装它们已连接到服务器(或者不是错误情况,您也想测试,不是吗?)并记录消息流量以证明您的代码已使用正确的端口连接到正确类型的服务器。

暂时不要使用线程,以简化测试。

I would try to introduce a factory into your existing code that purports to create socket objects. Then in a test pass in a mock factory which creates mock sockets which just pretend they've connected to a server (or not for error cases, which you also want to test, don't you?) and log the message traffic to prove that your code has used the right ports to connect to the right types of servers.

Try not to use threads just yet, to simplify testing.

软糯酥胸 2024-08-04 22:18:45

这取决于您的网络软件的分层方式以及您希望测试的详细程度,但在某些情况下将服务器设置和拆卸作为测试的一部分当然是可行的。 例如,当我开发 Python 日志包时(在它成为 Python 的一部分之前),我进行了一个测试(我没有使用 pyunit/unittest - 它只是一个临时脚本),它(在一个测试中)启动了四台服务器来侦听 TCP、UDP、HTTP 和 HTTP/SOAP 端口,然后向它们发送网络流量。 如果您有兴趣,可以在此处查看该发行版存档中要查看的相关测试脚本是 log_test.py。 当然,从那时起,Python 日志记录包已经取得了一些进展,但旧包仍然可以与 Python << 版本一起使用。 2.3且≥1.5.2。

It depends on how your network software is layered and how detailed you want your tests to be, but it's certainly feasible in some scenarios to make server setup and tear-down part of the test. For example, when I was working on the Python logging package (before it became part of Python), I had a test (I didn't use pyunit/unittest - it was just an ad-hoc script) which fired up (in one test) four servers to listen on TCP, UDP, HTTP and HTTP/SOAP ports, and then sent network traffic to them. If you're interested, the distribution is here and the relevant test script in the archive to look at is log_test.py. The Python logging package has of course come some way since then, but the old package is still around for use with versions of Python < 2.3 and >= 1.5.2.

浅笑依然 2024-08-04 22:18:45

我有一些测试用例,它们在设置中运行服务器并在拆卸中关闭它。 我不知道这是否是一种非常优雅的方式,但它对我有用。

我很高兴拥有它,它对我有很大帮助。

如果服务器初始化非常长,另一种选择是使用 ant 使其自动化。 ant 会在执行测试之前/之后运行/停止服务器。

有关 ant 和 python 的非常有趣的教程,请参阅此处

I've some test cases that run a server in the setUp and close it in the tearDown. I don't know if it is very elegant way to do it but it works of for me.

I am happy to have it and it helps me a lot.

If the server init is very long, an alternative would be to automate it with ant. ant would run/stop the server before/after executing the tests.

See here for very interesting tutorial about ant and python

小傻瓜 2024-08-04 22:18:45

您需要创建模拟套接字。 执行此操作的确切方法取决于您如何创建套接字,并且创建套接字生成器将是一个好主意。 您还可以使用 pymox 等模拟库来让您的生活更轻松。 它还可能消除仅仅为了测试目的而创建套接字生成器的需要。

使用 pymox,你会做这样的事情:

def test_connect(self):
    m = mox.Mox()
    m.StubOutWithMock(socket, 'socket')
    socket_mock = m.MockAnything()
    m.socket.socket(socket.AF_INET, socket.SOCK_STREAM).AndReturn(socket_mock)
    socket_mock.connect(('test_server1', 80))
    socket_mock.connect(('test_server2', 81))
    socket_mock.connect(('test_server3', 82))

    m.ReplayAll()
    code_to_be_tested()
    m.VerifyAll()
    m.UnsetStubs()

You would need to create mock sockets. The exact way to do that would depend on how you create sockets and creating a socket generator would be a good idea. You can also use a mocking library like pymox to make your life easier. It can also possibly eliminate the need to create a socket generator just for the sole purpose of testing.

Using pymox, you would do something like this:

def test_connect(self):
    m = mox.Mox()
    m.StubOutWithMock(socket, 'socket')
    socket_mock = m.MockAnything()
    m.socket.socket(socket.AF_INET, socket.SOCK_STREAM).AndReturn(socket_mock)
    socket_mock.connect(('test_server1', 80))
    socket_mock.connect(('test_server2', 81))
    socket_mock.connect(('test_server3', 82))

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