pytest-dev
pyfakefs
Python

Provides a fake file system that mocks the Python file system modules.

Last updated Jun 29, 2026
747
Stars
100
Forks
15
Issues
+1
Stars/day
Attention Score
64
Language breakdown
No language data available.
โ–ธ Files click to expand
README

pyfakefs PyPI version Python version Testsuite Documentation Status pre-commit.ci status PyPI - Downloads

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 creates a new empty in-memory file system at each test start, which replaces the real filesystem during the test. Think of pyfakefs as making a per-test temporary directory, except for an entire file system.

pyfakefs is tested with current versions of Linux, Windows and macOS.

Usage

There are several ways to invoke pyfakefs:

  • using the fs fixture with pytest
  • deriving from fakefilesystemunittest.TestCase for unittest
  • using fakefilesystemunittest.Patcher as context manager
  • using the fakefilesystemunittest.patchfs decorator on a single test
Refer to the usage documentation for more information.

Documentation

covers the latest released version for the current main branch

Features

Apart from automatically mocking most file-system functions, pyfakefs provides some additional features:
  • mapping files and directories from the real file system into the fake filesystem
  • configuration and tracking of the file system size
  • pause and resume of patching to be able to use the real file system inside a
test step
  • (limited) emulation of other OSes (Linux, macOS or Windows)
  • configuration to behave as if running as a non-root user while running
under root

Limitations

pyfakefs will not work with Python libraries that use C libraries to access the file system. This is because pyfakefs cannot patch the underlying C libraries' file access functions--the C libraries will always access the real file system. Refer to the documentation for more information about the limitations of pyfakefs.

History

pyfakefs.py 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. At last count, pyfakefs was used in over 20,000 Python tests at Google.

Google released pyfakefs to the public in 2011 as a Google Code project. Support for unittest and doctest was added in a fork by user jmcgeheeiv, further corrections were made in a separate fork with user shiffdane, and after the shutdown of Google Code was announced, John McGehee merged all three Google Code projects together here on GitHub. In 2022, the repository has been transferred to pytest-dev to ensure continuous maintenance.

ยฉ 2026 GitRepoTrend ยท pytest-dev/pyfakefs ยท Updated daily from GitHub