I am writing a unittest for a python script which does heavy file operations via os and hence I mocked some of the os functions. I noticed a strange behavior when patching the return value of os.path to return False via:
@pytest.fixture(autouse=True)
def init_mocks(monkeypatch):
monkeypatch.setattr(
mymodule.os.path, "exists", Mock(return_value=False))
My debugger (within VSCode) does not work at all or behaves really strange. Is there a way to mock os.path without affecting the debugger?
After some research I now understand, that monkeypatching affects the whole python runtime - and hence potentially also the debugger. (I found effects with os.path.exists as well as with os.path.join)
So, to patch os.path.exists while still being able to debug, I instead patched it via context manager:
@pytest.fixture(autouse=True)
def mock_os_path_exists():
with patch('hdbPackageComposer.os.path.exists', Mock(return_value=True)):
yield