Introduction¶
pyfakefs implements a fake file system that mocks the Python file system modules. Using pyfakefs, your tests operate on a fake file system in memory without touching the real disk. The software under test requires no modification to work with pyfakefs.
pyfakefs works with CPython 2.7, 3.4 and above, on Linux, Windows and OSX (MacOS), and with PyPy2 and PyPy3. Note that this is the last major release that still supports Python 2.7/PyPy2 and Python 3.4.
pyfakefs works with PyTest version 2.8.6 or above.
Installation¶
pyfakefs is available on PyPi. The latest released version can be installed from pypi:
pip install pyfakefs
The latest master can be installed from the GitHub sources:
pip install git+https://github.com/jmcgeheeiv/pyfakefs
Limitations¶
pyfakefs will not work with Python libraries that use C libraries to access the file system, because it cannot patch the underlying C libraries’ file access functions.
Depending on the kind of import statements used, pyfakefs may not patch the file system modules automatically. See Customizing Patcher and TestCase for more information and ways to work around this.
pyfakefs is only tested with CPython and newest PyPy versions, other Python implementations will probably not work.
Differences in the behavior in different Linux distributions or different MacOS or Windows versions may not be reflected in the implementation, as well as some OS-specific low-level file system behavior. The systems used for automatic tests in Travis.CI and AppVeyor are considered as reference systems.
History¶
pyfakefs was initially developed at Google by Mike Bland as a modest fake implementation of core Python modules. It was introduced to all of Google in September 2006. Since then, it has been enhanced to extend its functionality and usefulness. At last count, pyfakefs was used in over 2,000 Python tests at Google.
Google released pyfakefs to the public in 2011 as Google Code project pyfakefs:
Fork jmcgeheeiv-pyfakefs added direct support for unittest and doctest as described in Automatically find and patch file functions and modules
Fork shiffdane-jmcgeheeiv-pyfakefs added further corrections
After the shutdown of Google Code was announced, John McGehee merged all three Google Code projects together on GitHub where an enthusiastic community actively maintains and extends pyfakefs.