12

pyfakefs sounds very useful: it "was developed initially as a modest fake implementation of core Python modules to support moderately complex file system interactions, and was introduced Google-wide . . . in September 2006. Since then, it has received many (well-tested) contributions to extend its functionality and usefulness, and is used in over 900 Google Python tests."

Documentation appears to currently only be available within docstrings of the source code itself. It explains that the module provides the following elements:

  • FakeFile: Provides the appearance of a real file.
  • FakeDirectory: Provides the appearance of a real dir.
  • FakeFilesystem: Provides the appearance of a real directory hierarchy.
  • FakeOsModule: Uses FakeFilesystem to provide a fake os module replacement.
  • FakePathModule: Faked os.path module replacement.
  • FakeFileOpen: Faked file() and open() function replacements.

Documentation does not, however, explain how to effectively use these elements in testing.

What is the right way to ensure that a module under test accesses a fake filesystem and not the real one?

4
  • I think those APIs you discovered in docstrings are basically what you need to call in testing. So you get put it in individual test method, or setup, or setupClass (or tearDown*). What else is bothering you?
    – CppLearner
    May 9, 2013 at 19:35
  • I'm not clear on how to get the module under test to use the filesystem that I create using the fake_filesystem API. I don't think it makes sense to, for example, supply a file() function to my module's classes or functions just for the sake of enabling testing. I would rather expect to do this with "benevolent monkey-patching" in a way similar to this answer, but I'm not sure exactly what to monkey-patch.
    – intuited
    May 10, 2013 at 0:12
  • Monkey patching isn't really that easy if you have to do it from the beginning. You can check out mock library from Python and use it to monkey patch certain operations by supplying the object you want the class to return. I can give a little thought a day or two from now. Meanwhile I will see if others can give you some thoughts. An alternative is dependcy injection - but that works and makes sense if your function or method explicitly passes in some dependencies, for example, a file-like object or a database session object.
    – CppLearner
    May 10, 2013 at 4:07
  • The code I'm looking to test right now is doing somewhat complicated things with the file system, like scanning files and reading configurations. I want to verify that it's looking in the right places for those files, and that it's correctly interpreting the information from them. Also I'd like to know how to use this technique for future testing.
    – intuited
    May 11, 2013 at 5:57

1 Answer 1

1

As the original answer has been deleted, I will add a new one in case someone stumbles over this.
Disclaimer: I'm a maintainer of pyfakefs.

While at the time of writing the question this has not been the case, the answer is now mostly contained in the documentation. To summarize, there are 4 different possibilities how to emulate all file system functions using pyfakefs:

  • with pytest: just use the fs plugin
  • with unittest: derive your test from pyfakefs.fake_filesystem_unittest.TestCase and call self.setUpPyfakefs() in setUp
  • with other test frameworks: use pyfakefs.fake_filesystem_unittest.Patcher, either as context manager, or using setUp/tearDown on the patcher instance
  • as a standalone decorator: import pyfakefs.fake_filesystem_unittest.patchfs and use it as a decorator for your test function (@patchfs)

All these variants allow for some customization, and there are some convenience methods that can be called on the fake filesystem instance (like defining the size in bytes of the file system), but these are optional. Just using one of these methods will replace all Python filesystem calls to calls into a mocked (memory-based) filesystem.

Note also that there are some limitations that restrict the applicability of pyfakefs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.