Testing zmq sockets with unittest
<p>I'm trying to test my messaging response in one of my libraries.</p>

<pre><code>import unittest
from time import sleep

import zmq
from vexbot.messaging import Messaging


class TestMessaging(unittest.TestCase):
    def setUp(self):
        self.subscribe_address = 'tcp://127.0.0.1:4006'
        self.publish_address = 'tcp://127.0.0.1:4007'
        self.settings = {'subscribe_address': self.subscribe_address,
                         'publish_address': self.publish_address}

        context = zmq.Context()
        self.messaging = Messaging(self.settings, context)

        self.test_publish_socket = context.socket(zmq.PUB)
        self.test_publish_socket.connect(self.publish_address)

        self.test_subscribe_socket = context.socket(zmq.SUB)
        self.test_subscribe_socket.connect(self.subscribe_address)
        self.test_subscribe_socket.setsockopt_string(zmq.SUBSCRIBE, '')
        sleep(1)
</code></pre>

<p>My issue is that when I declare test methods in the above class, instead of calling the <code>setUp</code> function once and setting the state once, every test method tries recalls <code>setUp</code> to set it's own state and the address have already been snagged by the first instance.</p>

<p>How do I get around the multiprocess issues with the way <code>unittest</code> operates? Would it be better to call to a random socket for testing purposes?</p>