How do I patch the `pathlib.Path.exists()` method?
Question:
I want to patch the exists()
method of a pathlib.Path
object for a unit test but I have problems getting this to work.
What I am trying to do is this:
from unittest.mock import patch
from pathlib import Path
def test_move_basic():
p = Path('~/test.py')
with patch.object(p, 'exists') as mock_exists:
mock_exists.return_value = True
But it fails with:
AttributeError: 'PosixPath' object attribute 'exists' is read-only
.
Any ideas?
Answers:
You need to patch the class, not the instance. It is enough to patch the method on the Path
class, as it defines the exists
method for the whole of the pathlib
library (PosixPath
, WindowsPath
, PurePosixPath
and PureWindowsPath
all inherit the implementation):
>>> from unittest.mock import patch
>>> from pathlib import Path
>>> p = Path('~/test.py')
>>> with patch.object(Path, 'exists') as mock_exists:
... mock_exists.return_value = True
... p.exists()
...
True
>>> with patch.object(Path, 'exists') as mock_exists:
... mock_exists.return_value = False
... p.exists()
...
False
>>> with patch.object(Path, 'exists') as mock_exists:
... mock_exists.return_value = 'Anything you like, actually'
... p.exists()
...
'Anything you like, actually'
The pathlib
classes use __slots__
to keep their memory footprint low, which has the side-effect of their instances not supporting arbitrary attribute assignment.
@Martijn (sorry I can not add a comment yet).
Any idea what to do if there are multiple path objects that need to be patched differently?
from unittest.mock import patch
from pathlib import Path
def test_move_basic():
p0 = Path('a')
p1 = Path('b')
with patch.object(p0, 'exists', return_value=True):
with patch.object(p1, 'exists', return_value=False):
# ...
I want to patch the exists()
method of a pathlib.Path
object for a unit test but I have problems getting this to work.
What I am trying to do is this:
from unittest.mock import patch
from pathlib import Path
def test_move_basic():
p = Path('~/test.py')
with patch.object(p, 'exists') as mock_exists:
mock_exists.return_value = True
But it fails with:
AttributeError: 'PosixPath' object attribute 'exists' is read-only
.
Any ideas?
You need to patch the class, not the instance. It is enough to patch the method on the Path
class, as it defines the exists
method for the whole of the pathlib
library (PosixPath
, WindowsPath
, PurePosixPath
and PureWindowsPath
all inherit the implementation):
>>> from unittest.mock import patch
>>> from pathlib import Path
>>> p = Path('~/test.py')
>>> with patch.object(Path, 'exists') as mock_exists:
... mock_exists.return_value = True
... p.exists()
...
True
>>> with patch.object(Path, 'exists') as mock_exists:
... mock_exists.return_value = False
... p.exists()
...
False
>>> with patch.object(Path, 'exists') as mock_exists:
... mock_exists.return_value = 'Anything you like, actually'
... p.exists()
...
'Anything you like, actually'
The pathlib
classes use __slots__
to keep their memory footprint low, which has the side-effect of their instances not supporting arbitrary attribute assignment.
@Martijn (sorry I can not add a comment yet).
Any idea what to do if there are multiple path objects that need to be patched differently?
from unittest.mock import patch
from pathlib import Path
def test_move_basic():
p0 = Path('a')
p1 = Path('b')
with patch.object(p0, 'exists', return_value=True):
with patch.object(p1, 'exists', return_value=False):
# ...