[zeromq-dev] Testing and debugging
andrea crotti
andrea.crotti.0 at gmail.com
Wed Aug 15 11:50:34 CEST 2012
2012/8/14 Michel Pelletier <pelletier.michel at gmail.com>:
>
>> 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
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
Ok thanks for the answer, actually I use the "mock" library in general
for everything and it's great.
But in this case I don't see the advantage..
It's true that I don't need to test also zeromq and I could mock it,
but is it worth since I could just as easily use it??
For example now I wanted to check if the processes quit after
receiving a QUIT signal, and I was doing this way:
def start_worker(idx):
wr = worker.Worker(idx, 5)
wr.run()
class TestWorker(unittest.TestCase):
def setUp(self):
# set up all the zeromq channels and start some sample worker
# might use the interactive function for this
self.proc = Process(target=start_worker, args=(1, ))
self.proc.start()
def tearDown(self):
self.proc.terminate()
def test_kill_process(self):
context = zmq.Context()
zmq_sockets.set_context(context)
# turn on a subprocess
control_socket = zmq_sockets.control_send()
control_socket.send('QUIT')
# XXX: remove the dirty sleep if found a more clean way to synchronize
sleep(1)
self.assertTrue(not self.proc.is_alive())
Also because my worker class is now something like
class Worker:
"""Worker class, in charge of running the processes and report the
results back to the sink
"""
def __init__(self, idx, verbosity):
self.idx = idx
self.tests = [] # should a test object know about its area maybe?
self.context = zmq.Context()
zmq_sockets.set_context(self.context)
self.task_socket = zmq_sockets.task_recv()
...
So to actually mock something I should probably replace all the
sockets with something else, which in theory I could as simply as:
mock_socket = MagicMock(spec=zmq.core.socket.Socket)
But I'm still not sure there is an advantage..
More information about the zeromq-dev
mailing list