Make FakePathlibModule.Path compare equal to pathlib.Path
See original GitHub issueSometimes it happens that a test case compares a fake path to a real path - usually when you’ve created a pathlib.Path in pytest.mark.parametrize, like so:
def my_function():
return pathlib.Path()
@pytest.mark.parametrize('expected_result', [pathlib.Path()])
def test_my_function(fs, expected_result):
assert my_function() == expected_result
This fails with AssertionError: assert WindowsPath('.') == WindowsPath('.').
This could of course be avoided by passing in a string and converting it to a path inside the test, but in reality there isn’t always such a convenient workaround. Here’s a more realistic example, where an object with loads of attributes - some of which are Paths - is loaded from disk and compared to the expected result:
import pytest
from pathlib import Path
from my_module import MyClass
@pytest.mark.parametrize('attrs', [
{'foo': False,
'origin': Path(),
'bar': 17,
'paths': [Path('/'), Path('somewhere')]},
])
def test_serialization(fs, attrs):
obj = MyClass(**attrs)
obj.dump('myobj.dump')
loaded_obj = MyClass.load_file('myobj.dump')
assert vars(loaded_obj) == attrs
As I’m sure you can see, having to convert all of those attributes to Paths inside the test is rather annoying. For this reason it would be very convenient if fake Paths would compare equal to real Paths.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (6 by maintainers)

Top Related StackOverflow Question
Great idea, @mrbean-bremen. Done.
For now, I’ve worked around the problem by adding this monkey patch to my
conftest.py: