-
Notifications
You must be signed in to change notification settings - Fork 94
Description
Sometimes 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_resultThis 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) == attrsAs 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.