我已经使用自己的Fake实现成功地伪造了其他模块
monkeypatch
。但是在这种情况下,同时使用伪造实现或Monkeypatch
因subprocess.Popen
而失败:
monkeypatch
失败。结果还是真实的打开
Windows标题,而不是“ foo”。class TestController:
def test_get_all_windows(self, ctrl_fixture, monkeypatch):
def fake_communicate(a):
return "foo"
monkeypatch.setattr(subprocess.Popen, 'communicate', fake_communicate)
output = ctrl_fixture.get_all_windows()
print(output)
output
的结果仍然是真实的窗口
标题,而不是“ foo” class FakePopen(object):
def __init__(self, args, stdout=None):
super().__init__()
self.args = args
self.stdout = stdout
def communicate(self):
return "foo"
class TestController:
def test_get_all_windows(self, ctrl_fixture, monkeypatch, mocker):
def fake_communicate(a):
return "foo"
subprocess.Popen = FakePopen
subprocess.Popen.communicate = fake_communicate
output = ctrl_fixture.get_all_windows()
print(output)
我的功能是:
def get_all_windows(self):
all_windows = ""
all_windows_proc = Popen(["wmctrl", "-l"], stdout=PIPE)
all_windows_dirty, err = all_windows_proc.communicate()
for line in all_windows_dirty.splitlines():
windows_name = line.split(None, 3)[-1].decode()
all_windows += "{}\n".format(windows_name)
return all_windows
上面使用print
而不是assert
的测试导致我仍然要检查输出。
谢谢。
更新解决方案
根据munk条评论。非常感谢他。
两种解决方案均有效:
def test_get_all_windows_one(self, ctrl, monkeypatch):
window_title = b"0x006000ab 0 machine-name foo_window_title"
def fake_communicate(a):
return window_title, "err"
Lupr.controllers.controller.Popen = FakePopen
Lupr.controllers.controller.Popen.communicate = fake_communicate
output = ctrl.get_all_windows()
assert output == "foo_window_title\n"
def test_get_all_windows_two(self, ctrl, monkeypatch):
window_title = b"0x006000ab 0 machine-name foo_window_title"
def fake_communicate(a):
return window_title, "err"
monkeypatch.setattr(Lupr.controllers.controller, "Popen", FakePopen)
monkeypatch.setattr(
Lupr.controllers.controller.Popen, "communicate", fake_communicate
)
output = ctrl.get_all_windows()
assert output == "foo_window_title\n"
答案 0 :(得分:0)
您正在修补subprocess.Popen
,但是在测试功能中,您正在直接使用Popen
。您正在更改错误的符号表。
如果您的函数位于foo.py
中,则您想修补foo.Popen
或将其功能更改为使用subprocess.Popen
。