【问题标题】:Assert is function not passed in parameter is called断言是未传入参数的函数被调用
【发布时间】:2017-06-04 13:46:49
【问题描述】:

假设我有这样的功能:

def foo():
  bar()

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

【问题讨论】:

  • 您可以修改these function call counting decorators 以满足您的目的。
  • 有多种选择,具体取决于您的具体需求 - 搜索 python decorator function logging。试一试,如果遇到问题,请回来提出相关问题。

标签: python unit-testing testing python-mock


【解决方案1】:

您应该为此使用patch

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

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

mock_bar.assert_called_with('some_param')

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2023-01-20
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2016-06-13
    相关资源
    最近更新 更多