How to mock getenv in pytest?

Viewed 4372

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?

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')
Related