gpt4 book ai didi

python - 在 Python 单元测试中修补多个方法的首选方式

转载 作者:太空狗 更新时间:2023-10-29 16:57:37 25 4
gpt4 key购买 nike

我需要在测试调用第四个方法 (_handle_command) 在我的单元测试中。

通过查看 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装饰

@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

但我没有发现其中任何一个特别好。尤其是多个 patch.object 方法,需要仔细匹配参数顺序与嵌套装饰。

有没有我遗漏的方法,或者有什么方法可以使它更具可读性?当您需要在被测实例/类上修补多个方法时,您会怎么做?

最佳答案

不,您没有错过任何与您提议的真正不同的东西。

关于可读性,我喜欢装饰器方式,因为它从测试主体中删除了模拟内容……但这只是味道。

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

您只需要修补您的对象,将其用于您的所有测试:通常在测试中,您无法在调用之前对实例进行修补,在这些情况下,不能使用 create_autospec:您可以只修补方法的静态实例。

如果您对将实例传递给 assert_called_* 方法感到困扰,请考虑使用 ANY打破依赖。最后我写了数百个这样的测试,我从来没有遇到过参数顺序的问题。

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

from unittest.mock import patch

@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 都以 mock_ 前缀开头
  • 我更喜欢使用简单的 patch 调用和绝对路径:您正在做的事情清晰明了

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

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

[编辑]

我对这个问题思考了一会儿,发现困扰我的是:您正在尝试测试和感知私有(private)方法。当发生这种情况时,您应该问的第一件事是为什么?答案很可能是因为这些方法应该是私有(private)合作者的公共(public)方法 (that not my words)。

在那个新场景中,你应该感知私有(private)合作者,你不能只改变你的对象。您需要做的是修补一些其他类的静态实例。

关于python - 在 Python 单元测试中修补多个方法的首选方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com