【发布时间】:2021-09-17 07:29:31
【问题描述】:
我有一个方法可以执行以下操作:
import os
...
if not os.path.exists(dirpath):
os.makedirs(dirpath)
我正在尝试模拟 makedirs 和 path.exists,但是当我使用 patch 执行此操作时,模拟冲突:
@patch('os.makedirs')
@patch('os.path.exists')
def test_foo(self, makedirs, exists):
c = Config()
c.foo()
assert makedirs.called
assert exists.called
如果我禁用makedirs 或exists,它们都可以正常工作,但一起使用时会出现问题。
我也尝试过使用with patch('os.makedirs') as makedirs: 语法,它不会改变任何东西。
有谁知道他们为什么会发生冲突或者我可以做些什么来解决这个问题?
谢谢!
【问题讨论】:
标签: python mocking python-unittest python-unittest.mock