【发布时间】:2017-05-07 13:24:25
【问题描述】:
pytest 在monkeypatching 文档中有这个例子:
import os.path
def getssh(): # pseudo application code
return os.path.join(os.path.expanduser("~admin"), '.ssh')
def test_mytest(monkeypatch):
def mockreturn(path):
return '/abc'
monkeypatch.setattr(os.path, 'expanduser', mockreturn)
x = getssh()
assert x == '/abc/.ssh'
当我从 mockreturn 函数中删除 path 参数时,我得到了错误
def getssh(): # pseudo application code
> return os.path.join(os.path.expanduser("~admin"), '.ssh')
E TypeError: mockreturn() takes 0 positional arguments but 1 was given
我不明白是什么提供了位置参数?
另外,当我为 pathlib.Path.home() 重新实现相同的东西时,我不能在那里有这个参数path,否则它将不起作用。不幸的是,文档没有说明不祥的path 论点。
任何关于这里发生的魔法的照明都会非常有帮助!
【问题讨论】:
标签: python pytest monkeypatching