skillfed

pyfakefs

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

pyfakefs v6.2.0 4.5M downloads/30d#2,298 on PyPI750
Permissive license Apache-2.0 Active released

What it is and what it does

pyfakefs is a testing library that replaces the real filesystem with an in-memory fake one during test execution. It automatically mocks Python's file system modules so your tests can create, read, and manipulate files without touching disk. Each test gets a fresh empty filesystem, making tests isolated and fast. The library works with unittest (via TestCase subclass or Patcher context manager) and as a decorator on individual tests.

The package handles cross-platform testing by emulating Linux, Windows, and macOS filesystems, and includes features like mapping real files into the fake filesystem, tracking filesystem size, pausing/resuming patching, and simulating non-root user behavior. It does not work with libraries that use C-level filesystem access, since pyfakefs cannot patch underlying C library calls.

Use it for:

  • Test file I/O code (reading, writing, deleting) without creating temporary files on disk
  • Verify error handling for missing files or permission issues in a controlled environment
  • Run filesystem-heavy test suites in parallel without disk contention or cleanup overhead
  • Test cross-platform file path logic (Windows vs. POSIX) on a single machine
  • Simulate full filesystem scenarios (large directory trees, disk full conditions) cheaply in memory

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

pyfakefs mocks the Python file system modules to run tests against an in-memory fake filesystem instead of the real disk, with no changes required to the code under test.

Yes. pyfakefs is production-stable (Development Status 5), actively maintained, has no security vulnerabilities, and zero runtime dependencies. It solves a real testing problem—isolating filesystem operations—and is widely used. Install it if your test suite touches the filesystem.

Install

pyfakefs on PyPI

pip

pip install pyfakefs

uv

uv add pyfakefs

poetry

poetry add pyfakefs

Installing pyfakefs

Before you install

Low friction: pure Python wheel with no runtime dependencies. Actively maintained with recent releases; last commit 2026-08-13. Supports current Python versions on Linux, Windows, and macOS.

License in practice

Apache-2.0 (permissive): you can use, modify, and distribute pyfakefs freely in commercial and private projects, provided you include a copy of the license and state significant changes.

Quickstart

pip install pyfakefs

from pyfakefs import fake_filesystem_unittest

class TestWithFakeFS(fake_filesystem_unittest.TestCase):
    def setUp(self):
        self.setUpPyfakefs()
    
    def test_file_operations(self):
        self.fs.create_file('/tmp/test.txt', contents='hello')
        with open('/tmp/test.txt') as f:
            self.assertEqual(f.read(), 'hello')

Requires Python 3.10 or later; C-based filesystem libraries cannot be mocked by pyfakefs.

Verify before relying

  • Performance characteristics when filesystem size grows or test count scales
  • Compatibility with specific C-based libraries beyond the documented limitations
  • Whether all test frameworks and plugins work seamlessly with pyfakefs patching

Package facts

License Apache-2.0 (permissive)
Python support supports the current Python release (>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 124 days since the last release
Last repo commit
First released
Downloads 4,466,529/month — #2,298 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pyfakefs-6.2.0-py3-none-any.whl

Keywords: testing, filesystem, mocking, unittest, pytest

Development Status :: 5 - Production/StableEnvironment :: ConsoleFramework :: PytestIntended Audience :: DevelopersOperating System :: MacOSOperating System :: Microsoft :: WindowsOperating System :: POSIXProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: LibrariesTopic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: TestingTopic :: System :: Filesystems

Tags

mock filesystem for testingfake file system testingin-memory filesystem testingfile system mocking librarytest file operations without diskunittest filesystem mockingtemporary filesystem per test
test-isolationmockingfilesystem

More Libraries packages