当前位置: 动力学知识库 > 问答 > 编程问答 >

python - using pyunit on a network thread

问题描述:

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.

网友答案:

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.

网友答案:

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.

网友答案:

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

网友答案:

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()
分享给朋友:
您可能感兴趣的文章:
随机阅读: