My Python function creates pathlib.Path
objects. I want to mock this, so when the code will call pathlib.Path("/tmp/a").exists()
it will get True
, and when the code will call pathlib.Path("/tmp/b").exists()
it will get False
.
I tried this:
import pathlib
from unittest.mock import patch
def my_side_effect(*args, **kwargs):
print (f" args = {args} , kwargs={kwargs}")
return True
with patch.object(pathlib.Path, 'exists') as mock_exists:
mock_exists.side_effect = my_side_effect
print(pathlib.Path("a").exists())
Output:
args = () , kwargs={}
True
As you can see, my side effect is not getting any args/kwargs based on the path.