[zeromq-dev] Testing and debugging

Michel Pelletier pelletier.michel at gmail.com
Tue Aug 14 17:24:50 CEST 2012


On Tue, Aug 14, 2012 at 7:34 AM, andrea crotti
<andrea.crotti.0 at gmail.com> wrote:
> 2012/8/13 Michel Pelletier <pelletier.michel at gmail.com>:

> Thanks for the help, any example of how to mock the zmq messages maybe?
>
> I was trying to find a way and I'm ending up mocking the whole module,
> but it doesn't really work this way:
>

<snip>

> And I'm not sure it's worth actually to mock in this way, instead I
> could just send message with "inproc" instead of the TCP connections..

You should look at a mocking library to do the actual mocked objects
for you.  flexmock, for example, is one I like, although there are
several.  Here's an off the cuff example:


from flexmock import flexmock


def do(pub):
    pub.send('hi there!')


def test_do():
    mock_pub = flexmock()
    (flexmock(mock_pub).
     should_receive('send').
     with_args('hi there!').
     once())

    do(mock_pub)


Put it in test.py and run it with "nosetests test.py".  Note that no
real zmq objects are actually created, the zmq module isn't even
imported.  The point is to isolate an test your logic as much as
possible.  You don't need to test that zmq works to test your logic.
Also flexmock will take care of asserting that your method was called
once with the correct args, there's no need to reimplement that.

-Michel



More information about the zeromq-dev mailing list