Skip to content

Make FakePathlibModule.Path compare equal to pathlib.Path #478

@Aran-Fey

Description

@Aran-Fey

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_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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions