Python3 模拟设置
Python3 mock on setup
我正在尝试在设置时模拟一些第 3 部分库,这样我就可以假装它在我的代码中按预期工作。
我能够在本地模拟它,我在函数本身上配置了所有 returns
class MockConnecton:
def __init__(self):
self._ch = Mock()
def channel(self):
return self._ch
class QEmiterTest(unittest.TestCase):
@patch('task_queues.queue.pika.BlockingConnection')
@patch('task_queues.queue.pika.ConnectionParameters')
def test_emiter(self,mock_params,mock_block):
config = {
'host':'mq',
'exchange':'test'
}
params = {"FOO":"BAR"}
mock_params.return_value = params
conn = MockConnecton()
mock_conn = Mock(wraps=conn)
mock_block.return_value = mock_conn
emitter = QEmitter(config['host'],config['exchange'])
mock_params.assert_called_with(config['host'])
mock_block.assert_called_with(params)
mock_conn.channel.assert_called_with()
conn._ch.exchange_declare.assert_called_with(exchange=config['exchange'],type='topic')
但是当我尝试从这种方法转向使用 mock start/stop 的更清洁的方法时,我收到断言错误:
AttributeError: '_patch' object has no attribute 'assert_called_with'
我正在尝试这样移植它
class QEmiterTest(unittest.TestCase):
def setUp(self):
mock_params = patch('task_queues.queue.pika.ConnectionParameters')
mock_block = patch('task_queues.queue.pika.BlockingConnection')
self.params_ret = {"FOO":"BAR"}
mock_params.return_value = self.params_ret
conn = MockConnecton()
self.mock_conn = Mock(wraps=conn)
mock_block.return_value = self.mock_conn
self.patch_params = mock_params
self.patch_block = mock_block
self.patch_params.start()
self.patch_block.start()
def test_emiter(self):
config = {
'host':'mq',
'exchange':'test'
}
emitter = QEmitter(config['host'],config['exchange'])
self.patch_params.assert_called_with(config['host'])
self.patch_block.assert_called_with(self.params_ret)
self.mock_conn.channel.assert_called_with()
self.mock_conn._ch.exchange_declare.assert_called_with(exchange=config['exchange'],type='topic')
def tearDown(self):
self.patch_params.stop()
self.patch_block.stop()
我可能不完全理解开始和停止,我假设在设置时它会应用补丁,并且通过它的参考我将能够做出断言。我也欢迎任何关于如何使多个模拟更清洁的建议
patch
对象是补丁而不是模拟。 mock
框架有两个主要职责:
- 模拟对象用于记录通话并跟随您的剧本
- 用于将引用替换为可用于传感器或模拟某些行为的内容的补丁方法和对象
很多时候我们可以使用补丁来安装模拟...但补丁不是模拟。
patch.start()
return 用于修补原始参考的新参考(通常是模拟)。
def setUp(self):
self.params_ret = {"FOO":"BAR"}
self.mock_conn = Mock(wraps=conn)
self.patch_params = patch('task_queues.queue.pika.ConnectionParameters', return_value = self.params_ret)
self.patch_block = patch('task_queues.queue.pika.BlockingConnection', return_value=self.mock_conn)
mock_params = self.patch_params.start()
mock_block = self.patch_block.start()
我正在尝试在设置时模拟一些第 3 部分库,这样我就可以假装它在我的代码中按预期工作。
我能够在本地模拟它,我在函数本身上配置了所有 returns
class MockConnecton:
def __init__(self):
self._ch = Mock()
def channel(self):
return self._ch
class QEmiterTest(unittest.TestCase):
@patch('task_queues.queue.pika.BlockingConnection')
@patch('task_queues.queue.pika.ConnectionParameters')
def test_emiter(self,mock_params,mock_block):
config = {
'host':'mq',
'exchange':'test'
}
params = {"FOO":"BAR"}
mock_params.return_value = params
conn = MockConnecton()
mock_conn = Mock(wraps=conn)
mock_block.return_value = mock_conn
emitter = QEmitter(config['host'],config['exchange'])
mock_params.assert_called_with(config['host'])
mock_block.assert_called_with(params)
mock_conn.channel.assert_called_with()
conn._ch.exchange_declare.assert_called_with(exchange=config['exchange'],type='topic')
但是当我尝试从这种方法转向使用 mock start/stop 的更清洁的方法时,我收到断言错误:
AttributeError: '_patch' object has no attribute 'assert_called_with'
我正在尝试这样移植它
class QEmiterTest(unittest.TestCase):
def setUp(self):
mock_params = patch('task_queues.queue.pika.ConnectionParameters')
mock_block = patch('task_queues.queue.pika.BlockingConnection')
self.params_ret = {"FOO":"BAR"}
mock_params.return_value = self.params_ret
conn = MockConnecton()
self.mock_conn = Mock(wraps=conn)
mock_block.return_value = self.mock_conn
self.patch_params = mock_params
self.patch_block = mock_block
self.patch_params.start()
self.patch_block.start()
def test_emiter(self):
config = {
'host':'mq',
'exchange':'test'
}
emitter = QEmitter(config['host'],config['exchange'])
self.patch_params.assert_called_with(config['host'])
self.patch_block.assert_called_with(self.params_ret)
self.mock_conn.channel.assert_called_with()
self.mock_conn._ch.exchange_declare.assert_called_with(exchange=config['exchange'],type='topic')
def tearDown(self):
self.patch_params.stop()
self.patch_block.stop()
我可能不完全理解开始和停止,我假设在设置时它会应用补丁,并且通过它的参考我将能够做出断言。我也欢迎任何关于如何使多个模拟更清洁的建议
patch
对象是补丁而不是模拟。 mock
框架有两个主要职责:
- 模拟对象用于记录通话并跟随您的剧本
- 用于将引用替换为可用于传感器或模拟某些行为的内容的补丁方法和对象
很多时候我们可以使用补丁来安装模拟...但补丁不是模拟。
patch.start()
return 用于修补原始参考的新参考(通常是模拟)。
def setUp(self):
self.params_ret = {"FOO":"BAR"}
self.mock_conn = Mock(wraps=conn)
self.patch_params = patch('task_queues.queue.pika.ConnectionParameters', return_value = self.params_ret)
self.patch_block = patch('task_queues.queue.pika.BlockingConnection', return_value=self.mock_conn)
mock_params = self.patch_params.start()
mock_block = self.patch_block.start()