Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to mock methods like `pathlib.Path.is_dir`?
I used to have some testing code for mocking a simple directory structure when working with pathlib
. I recently tried to run these tests to learn that some of the internals in pathlib
have changed in recent Python versions, breaking my mocking setup. I managed to resolve some of the issues already, but I got stuck on an issue that feels like it shouldn't be one.
Concretely, I have been mocking pathlib.Path.is_dir
as follows:
from pathlib import Path
from unittest import mock
some_path = Path("/some/path/")
with mock.patch("pathlib.Path.is_dir") as m_is_dir:
m_is_dir.side_effect = lambda p: p.name == ""
print(Path.is_dir(some_path)) # this is just a placeholder
Now, the problem is that the code in newer Python versions (internally) seems to be using some_path.is_dir()
instead. As a result, I get a TypeError
about a missing positional argument p
for my lambda expression. This can be simulated by the following code
with mock.patch("pathlib.Path.is_dir") as m_is_dir:
m_is_dir.side_effect = lambda p: p.name == ""
print(some_path.is_dir()) # this is just a placeholder
This can be fixed by rewriting the mocking function
with mock.patch("pathlib.Path.is_dir") as m_is_dir:
m_is_dir.side_effect = lambda: some_path.name == ""
print(some_path.is_dir()) # this is just a placeholder
but of course that breaks the tests for the older versions again.
I have tried to patch some_path.is_dir
directly, using
with mock.patch("pathlib.Path.is_dir") as m_is_dir:
m_is_dir.side_effect = lambda p: p.name == ""
some_path.is_dir = lambda: m_is_dir(some_path)
print(some_path.is_dir()) # this is just a placeholder
but that resulted in an AttributeError
because is_dir
is read-only.
I actually would have assumed that patching Path.is_dir(p)
(indirectly) patch the bound method p.is_dir
as well, but that does not seem to be the case. So that leaves me with the question: is there a robust way to patch methods that can be called either way? Or more concretely: how can I patch Path.is_dir
so that the following works?
from pathlib import Path
from unittest import mock
some_path = Path("/some/path/")
with mock.patch(???) as m_is_dir:
m_is_dir.side_effect = lambda p: p.name == ""
print(Path.is_dir(some_path))
print(some_path.is_dir())
It might be that there is an obvious solution, but I am in too deep and I don't seem to be able to find it, so any help would be greatly appreciated.
1 answer
It turns out that this is exactly what autospeccing is for. Using
from pathlib import Path
from unittest import mock
some_path = Path("/some/path/")
with mock.patch("pathlib.Path.is_dir", autospec=True) as m_is_dir:
m_is_dir.side_effect = lambda p: p.name == ""
print(Path.is_dir(some_path))
print(some_path.is_dir())
works as expected.
It was this link to an older version of the docs that put me on the right track.
0 comment threads