pythonpython-3.xpyfakefs

Testing isinstance(PosixPath) using pytest with pyfakefs


In my code, I test if config_location is an instance of PosixPath or None:

if isinstance(self.config_location, (PosixPath, type(None))):
    ...

This works fine when run with the normal python interpreter, but fails during my pytest tests using pyfakefs, as the pyfakefs object is of type pyfakefs.fake_pathlib.FakePathlibModule.PosixPath. The obvious solution here would be to import pyfakefs into the tested code and add pyfakefs.fake_pathlib.FakePathlibModule.PosixPath to the isinstance tuple, but this approach seems very clunky to me.

There has to be a better way ?


Solution

  • Testing for PurePath instead works.