【发布时间】:2017-03-21 19:36:10
【问题描述】:
我正在尝试模拟 subprocess.Popen。但是,当我运行以下代码时,模拟完全被忽略了,我不知道为什么
测试代码:
def test_bring_connection_up(self):
# All settings should either overload the update or the run method
mock_popen = MagicMock()
mock_popen.return_value = {'communicate': (lambda: 'hello','world')}
with patch('subprocess.Popen', mock_popen):
self.assertEqual(network_manager.bring_connection_up("test"), "Error: Unknown connection: test.\n")
模块代码:
from subprocess import Popen, PIPE
# ........
def list_connections():
process = Popen(["nmcli", "-t", "-fields", "NAME,TYPE", "con", "list"], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate() # <--- Here's the failure
return stdout
【问题讨论】:
-
如果您将导入从
from subprocess import Popen更改为import subprocess,然后使用subprocess.Popen代替Popen,该怎么办? -
我得到和上面一样的结果
标签: python unit-testing mocking