Skip to content

Instantly share code, notes, and snippets.

@bluca
Created December 16, 2016 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluca/5028bae1768c01d587dabe51f7877750 to your computer and use it in GitHub Desktop.
Save bluca/5028bae1768c01d587dabe51f7877750 to your computer and use it in GitHub Desktop.
python zmq unit test mock
import zmq
import unittest
from unittest.mock import patch
from unittest.mock import MagicMock
class MockZmqSocket(MagicMock):
def __init__(self, **kwds):
super().__init__(**kwds)
self._connected = False
def connect(self, endpoint):
assert not self._connected
assert endpoint != ""
self._connected = True
def close(self):
assert self._connected
self._connected = False
class MockZmqContext(MagicMock):
def __init__(self, **kwds):
super().__init__(**kwds)
self._destroyed = False
self.IPV6 = None
self.RCVTIMEO = None
def socket(self, sock_type):
assert not self._destroyed
assert self.IPV6 == 1
assert self.RCVTIMEO is not None
assert sock_type == zmq.REQ
return MockZmqSocket()
def destroy(self, linger):
assert not self._destroyed
self._destroyed = True
@patch.object(zmq.Context, "instance", MockZmqContext)
class TestFoo(unittest.TestCase):
def test_bar(self):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment