gpt4 book ai didi

python - 断言函数没有传入参数被调用

转载 作者:行者123 更新时间:2023-11-28 21:24:22 25 4
gpt4 key购买 nike

假设我有一个这样的函数:

def foo():
bar()

当我没有将 bar 作为参数传递时,有什么方法可以测试 bar 是否被调用?我的团队使用 Python 3.6 和 3.5

最佳答案

你应该使用 patch为此:

@patch('path.to.bar')
def test_foo(self, mock_bar):
foo()
self.assertTrue(mock_bar.called)

您还可以像这样测试调用函数的值:

mock_bar.assert_called_with('some_param')

希望对您有所帮助。

关于python - 断言函数没有传入参数被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44354743/

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