Replies: 10 comments 3 replies
-
Thanks for the report! I see your problem, and I think there is an easy fix for this (overwriting |
Beta Was this translation helpful? Give feedback.
-
Hmm, I think that would probably be sufficient, yes. I'm not sure if it's a good idea to have an asymmetrical |
Beta Was this translation helpful? Give feedback.
-
Well, the question is where to do this. The actual problem is that the |
Beta Was this translation helpful? Give feedback.
-
For now, I've worked around the problem by adding this monkey patch to my # monkeypatch Path.__eq__ so that pyfakefs FakePaths compare equal to real pathlib.Paths
from pathlib import Path
from pyfakefs.fake_pathlib import FakePath
# Path somehow gets monkeypatched during testing, so in order to have access
# to the original class we'll simply create an instance of it
PATH = object.__new__(Path)
def path_eq(self, other):
Path = type(PATH)
if isinstance(other, (Path, FakePath)):
return str(self) == str(other)
return super(Path, self).__eq__(other)
Path.__eq__ = path_eq |
Beta Was this translation helpful? Give feedback.
-
Yes, this is a valid solution, at least for your case. I have been thinking about adding the same monkeypatching to the pytest plugin, but I'm somewhat reluctant to do this, as generally there could be other non-patched fs calls in a |
Beta Was this translation helpful? Give feedback.
-
Getting back to this, I now think that the correct way to resolve this would be to reload the sut module during the test (for example using import pytest
from pyfakefs.fake_filesystem_unittest import Patcher
import sut
@pytest.fixture
def fs_reload_sut():
with Patcher(modules_to_reload=[sut]) as p:
yield p.fs |
Beta Was this translation helpful? Give feedback.
-
@jmcgeheeiv - instead of closing the issue, I would like to to convert this (and other issues that may be helpful) into a Q/A discussion issue. If you agree, could you enable discussions in the pyfakefs configuration? |
Beta Was this translation helpful? Give feedback.
-
Great idea, @mrbean-bremen. Done. |
Beta Was this translation helpful? Give feedback.
-
I'm getting the same error. I've put in the pytest.fixture above (tweaked for my code). @pytest.fixture
def my_fs() :
with Patcher( modules_to_reload=[my_module] ) as p :
yield p.fs
The types for the two variables are:
Other than the fixture above, is there anything else I need to do? Actually, I'm not sure why it is |
Beta Was this translation helpful? Give feedback.
-
I have a "work-around" (or maybe it is the correct way to do things?) My module was instantiating a default instance of a class at the module scope. It is kind of used as a singleton. # @attr.s( frozen=True )
@attr.s( slots=True )
class Config_Default( Config_Base ) :
"""A frozen (immutable) version of the `Config_Base` class."""
pass
#! An instance of the (frozen) default config
CONFIG_DEFAULT = Config_Default() If I instantiate it in in my pytest function then everything works - presumably as the def test_defaults(
self,
mocker, #! pytest-mock test fixture
my_fs, #! my pyfakefs test fixture
) :
CONFIG_DEFAULT = Config_Default()
config = self.get_config( mocker=mocker, fs=my_fs )
assert config.foo_path == CONFIG_DEFAULT.foo_path
... Actually this works without the custom test fixture !! I thought the custom fixture was supposed to reload everything, but maybe I am misunderstanding how it works ? |
Beta Was this translation helpful? Give feedback.
-
Sometimes it happens that a test case compares a fake path to a real path - usually when you've created a
pathlib.Path
inpytest.mark.parametrize
, like so: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:
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.
Beta Was this translation helpful? Give feedback.
All reactions