Python单元tes中修补多个方法的首选方法

2024-06-07 01:40:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的单元测试中测试对第四个方法(_handle_command)的调用之前,我需要用模拟方法修补三个方法(_send_reply_reset_watchdog_handle_set_watchdog)。

通过查看mock包的文档,我可以采用以下几种方法:

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()

使用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()

有多个装饰{}

@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()

使用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)

就我个人而言,我认为最后一个是最清晰的阅读,并不会导致可怕的长线,如果模仿方法的数量增长。它还避免了将simulator作为第一个(self)参数传入assert_called_once_with

但我觉得没有特别好的。尤其是multiplepatch.object方法,它需要仔细地将参数顺序与嵌套装饰匹配起来。

有没有什么方法我错过了,或者有什么方法可以让它更容易阅读?当您需要修补被测实例/类上的多个方法时,您会怎么做?


Tags: 方法selfsendreplymockcommandwatchdogreset
1条回答
网友
1楼 · 发布于 2024-06-07 01:40:18

不,你没有错过任何与你的提议完全不同的东西。

关于可读性,我喜欢decorator方式,因为它从测试体中移除了模拟的东西。。。但这只是味道。

您是对的:如果通过autospec=True修补方法的静态实例,则必须在assert_called_*系列检查方法中使用self。但是,您的案例只是一个小类,因为您确切地知道需要修补的对象,并且您不需要为修补程序提供测试方法以外的其他上下文。

你只需要修补你的对象使用它来完成你的所有测试:通常在测试中,你不能在调用之前修补实例,在这些情况下,不能使用create_autospec:你可以只修补方法的静态实例。

如果您对将实例传递给assert_called_*方法感到不安,请考虑使用^{}来断开依赖关系。最后,我写了几百个这样的测试,我从来没有遇到过关于参数顺序的问题。

我在你的测试中的标准方法是

@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()
  • 修补程序超出了测试方法代码
  • 每个模拟都以mock_前缀开头
  • 我更喜欢使用简单的patch调用和绝对路径:它是清晰和整洁的

最后:也许创建simulator并停止它是setUp()tearDown()责任和测试应该考虑到只是修补一些方法和做检查。

我希望这个答案是有用的,但是这个问题没有唯一有效的答案,因为可读性不是一个绝对的概念,依赖于读者。此外,即使是关于一般情况的标题,问题示例也是关于应该在其中修补要测试的对象的方法的特定问题类。

[编辑]

我想了一会儿这个问题,我发现困扰我的是:你试图测试和感觉私人方法。当这种情况发生时,你应该问的第一件事是为什么?答案很可能是因为这些方法应该是私有协作者的公共方法(that not my words)。

在这个新的场景中,你应该了解到私人合作者,你不能仅仅改变你的对象。您需要做的是修补其他一些类的静态实例。

相关问题 更多 >