如何在pytest中模拟getenv?

rac*_*iri 6 python pytest python-2.7

在我的一些测试中,我需要模拟一些函数调用,例如os.getenv,我尝试结合使用模拟.patch,但是我猜pytest和patch不能并存,我该怎么做?

wim*_*wim 7

不用理会mock.patch。由于正在使用pytest,因此应使用文档中提到的灯具:

def test_thing(monkeypatch):
    monkeypatch.setenv('VARNAME', 'var_value')
Run Code Online (Sandbox Code Playgroud)