Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock getenv in pytest?

In some of my test, I need to mock some function calls such as os.getenv, I tried to combine mock.patch but I guess pytest and patch don't go hand in hand, how can I do this?

like image 233
rachid el kedmiri Avatar asked Sep 19 '17 16:09

rachid el kedmiri


1 Answers

Don't bother with mock.patch. Since you are using pytest, you should use the fixture mentioned in the documentation:

def test_thing(monkeypatch):
    monkeypatch.setenv('VARNAME', 'var_value')
like image 194
wim Avatar answered Sep 24 '22 07:09

wim