【问题标题】:how to mock two functions using unittests mock.patch如何使用单元测试 mock.patch 模拟两个函数
【发布时间】:2021-06-16 15:44:38
【问题描述】:

我需要在我的 API post 方法中模拟两个函数。 只模拟一个函数效果很好,但是当我需要模拟两个函数时,我不知道该怎么做?

@unittest.mock.patch('path.to.first.function.thefirstfunction')
def test_connector_post(mock_thefirstfunction):
... 

有什么想法吗?

【问题讨论】:

  • 你可能可以堆叠装饰器!
  • 感谢@ti7,它正在工作

标签: python testing mocking


【解决方案1】:

堆叠装饰器!

@unittest.mock.patch("functionA")
@unittest.mock.patch("functionB")
def test_foo(...):
    ... 

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 2019-09-13
    • 2016-07-10
    • 2021-11-10
    • 2020-10-27
    • 2022-12-07
    相关资源
    最近更新 更多