est*_*tan 17 python unit-testing mocking
在测试对我的单元测试中的第四个方法()的调用之前_send_reply,我需要使用mock方法修补三个方法(,_reset_watchdog和_handle_set_watchdog)_handle_command.
从查看模拟包的文档,有几种方法可以解决它:
随着patch.multiple作为装饰
@patch.multiple(MBG120Simulator,
_send_reply=DEFAULT,
_reset_watchdog=DEFAULT,
_handle_set_watchdog=DEFAULT,
autospec=True)
def test_handle_command_too_short_v1(self,
_send_reply,
_reset_watchdog,
_handle_set_watchdog):
simulator = MBG120Simulator()
simulator._handle_command('XA99')
_send_reply.assert_called_once_with(simulator, 'X?')
self.assertFalse(_reset_watchdog.called)
self.assertFalse(_handle_set_watchdog.called)
simulator.stop()
Run Code Online (Sandbox Code Playgroud)
随着patch.multiple如上下文管理器
def test_handle_command_too_short_v2(self):
simulator = MBG120Simulator()
with patch.multiple(simulator,
_send_reply=DEFAULT,
_reset_watchdog=DEFAULT,
_handle_set_watchdog=DEFAULT,
autospec=True) as mocks:
simulator._handle_command('XA99')
mocks['_send_reply'].assert_called_once_with('X?')
self.assertFalse(mocks['_reset_watchdog'].called)
self.assertFalse(mocks['_handle_set_watchdog'].called)
simulator.stop()
Run Code Online (Sandbox Code Playgroud)
有多个patch.objectdecoratorations
@patch.object(MBG120Simulator, '_send_reply', autospec=True)
@patch.object(MBG120Simulator, '_reset_watchdog', autospec=True)
@patch.object(MBG120Simulator, '_handle_set_watchdog', autospec=True)
def test_handle_command_too_short_v3(self,
_handle_set_watchdog_mock,
_reset_watchdog_mock,
_send_reply_mock):
simulator = MBG120Simulator()
simulator._handle_command('XA99')
_send_reply_mock.assert_called_once_with(simulator, 'X?')
self.assertFalse(_reset_watchdog_mock.called)
self.assertFalse(_handle_set_watchdog_mock.called)
simulator.stop()
Run Code Online (Sandbox Code Playgroud)
使用手动替换方法 create_autospec
def test_handle_command_too_short_v4(self):
simulator = MBG120Simulator()
# Mock some methods.
simulator._send_reply = create_autospec(simulator._send_reply)
simulator._reset_watchdog = create_autospec(simulator._reset_watchdog)
simulator._handle_set_watchdog = create_autospec(simulator._handle_set_watchdog)
# Exercise.
simulator._handle_command('XA99')
# Check.
simulator._send_reply.assert_called_once_with('X?')
self.assertFalse(simulator._reset_watchdog.called)
self.assertFalse(simulator._handle_set_watchdog.called)
Run Code Online (Sandbox Code Playgroud)
我个人认为最后一个是最清楚的,如果模拟方法的数量增长,不会导致可怕的长行.它还避免了必须simulator作为first(self)参数传入assert_called_once_with.
但我发现它们中的任何一个都不是特别好.特别是多patch.object方法,需要仔细匹配参数顺序与嵌套装饰.
是否有一些我错过的方法,或者是一种让它更具可读性的方法?当您需要在测试的实例/类上修补多个方法时,您会怎么做?
Mic*_*ico 10
不,你没有错过任何与你提出的建议完全不同的东西.
关于可读性我的品味是装饰方式,因为它从测试体中移除了嘲弄的东西......但它只是味道.
你是对的:如果修补方法的静态实例,则autospec=True必须在assert_called_*族检查方法中使用self .但是你的情况只是一个小类,因为你确切知道你需要修补什么对象,而你的补丁并不需要其他上下文而不是测试方法.
您需要修补您的对象使用它进行所有测试:通常在测试中,您无法在执行调用之前修改实例,并且在这些情况下create_autospec无法使用:您可以修改方法的静态实例.
如果您对实例传递assert_called_*方法感到困扰,请考虑使用它ANY来破坏依赖关系.最后我写了数百个这样的测试,我从来没有关于参数顺序的问题.
我测试的标准方法是
@patch('mbgmodule.MBG120Simulator._send_reply', autospec=True)
@patch('mbgmodule.MBG120Simulator._reset_watchdog', autospec=True)
@patch('mbgmodule.MBG120Simulator._handle_set_watchdog', autospec=True)
def test_handle_command_too_short(self,mock_handle_set_watchdog,
mock_reset_watchdog,
mock_send_reply):
simulator = MBG120Simulator()
simulator._handle_command('XA99')
# You can use ANY instead simulator if you don't know it
mock_send_reply.assert_called_once_with(simulator, 'X?')
self.assertFalse(mock_reset_watchdog.called)
self.assertFalse(mock_handle_set_watchdog_mock.called)
simulator.stop()
Run Code Online (Sandbox Code Playgroud)
mock_前缀开头patch调用和绝对路径:它清楚而整洁,你在做什么最后:也许创建simulator并停止它setUp(),tearDown()责任和测试应该只考虑修补一些方法并进行检查.
我希望答案是有用的,但问题没有一个独特的有效答案,因为可读性不是一个绝对的概念,取决于读者.此外,即使标题谈到一般情况,问题的例子也是关于特定的问题类,你应该修补对象的方法进行测试.
[编辑]
我对这个问题有一段时间了,我发现了什么困扰我:你试图测试和感知私人方法.当发生这种情况时,首先要问的是为什么?答案很有可能是因为这些方法应该是私人合作者的公共方法(这不是我的话).
在新的场景中,您应该感知私人协作者,而您无法仅仅更改您的对象.您需要做的是修补其他一些类的静态实例.
| 归档时间: |
|
| 查看次数: |
10855 次 |
| 最近记录: |