简体   繁体   English

如何在pytest中模拟getenv?

[英]How to mock getenv in pytest?

在我的一些测试中,我需要模拟一些函数调用,如os.getenv,我试图结合mock.patch但我猜pytest和补丁不是齐头并进,我怎么能这样做?

Don't bother with mock.patch . 不要打扰mock.patch Since you are using pytest , you should use the fixture mentioned in the documentation : 由于您使用的是pytest ,您应该使用文档中提到的夹具:

def test_thing(monkeypatch):
    monkeypatch.setenv('VARNAME', 'var_value')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM