Created
December 16, 2016 16:41
-
-
Save bluca/5028bae1768c01d587dabe51f7877750 to your computer and use it in GitHub Desktop.
python zmq unit test mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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