{"id":1647,"library":"pyfakefs","title":"pyfakefs","description":"pyfakefs implements a fake file system that mocks the Python file system modules. It allows tests to operate on an in-memory file system without touching the real disk, requiring no modification to the software under test. pyfakefs supports pytest, unittest, and can be used as a context manager. It is actively maintained with frequent releases to support new Python versions and fix bugs.","status":"active","version":"6.1.6","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pyfakefs","tags":["testing","mocking","filesystem","pytest","unittest","development"],"install":[{"cmd":"pip install pyfakefs","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.10 or newer.","package":"python","optional":false},{"reason":"Provides a convenient 'fs' fixture for pytest users (pytest >= 6.2.5).","package":"pytest","optional":true}],"imports":[{"note":"The 'fs' fixture is automatically provided by the pyfakefs pytest plugin.","symbol":"pytest_fixture","correct":"import pytest\ndef test_something(fs):"},{"note":"For unittest integration, inherit from TestCase and call self.setUpPyfakefs() and self.tearDownPyfakefs().","symbol":"TestCase","correct":"from pyfakefs.fake_filesystem_unittest import TestCase"},{"note":"Use Patcher as a context manager for patching specific blocks of code.","symbol":"Patcher","correct":"from pyfakefs.fake_filesystem_unittest import Patcher"},{"note":"For direct programmatic access to the fake filesystem object without a test runner integration.","symbol":"FakeFilesystem","correct":"from pyfakefs.fake_filesystem import FakeFilesystem"}],"quickstart":{"code":"import os\nimport pytest\n\ndef test_file_operations(fs):\n    # The 'fs' fixture provides a fake file system\n    # All os.path and file operations will use this fake system\n    assert not os.path.exists(\"/test_dir\")\n    os.mkdir(\"/test_dir\")\n    assert os.path.exists(\"/test_dir\")\n\n    file_path = \"/test_dir/my_file.txt\"\n    with open(file_path, \"w\") as f:\n        f.write(\"Hello, pyfakefs!\")\n\n    assert os.path.exists(file_path)\n    with open(file_path, \"r\") as f:\n        content = f.read()\n    assert content == \"Hello, pyfakefs!\"\n\n    os.remove(file_path)\n    assert not os.path.exists(file_path)","lang":"python","description":"This example demonstrates basic file operations using the `fs` pytest fixture, which automatically sets up and tears down an in-memory fake filesystem for your test. Simply run `pytest your_test_file.py`."},"warnings":[{"fix":"Upgrade your Python environment to 3.10 or newer, or pin pyfakefs to a version prior to 6.0.0 (e.g., `pyfakefs<6.0.0`).","message":"pyfakefs version 6.0.0 and above dropped support for Python versions older than 3.10. Ensure your project runs on Python 3.10+ when upgrading.","severity":"breaking","affected_versions":"6.0.0+"},{"fix":"Migrate your code to use the standard `os.scandir` and `pathlib.Path` modules, which pyfakefs continues to patch.","message":"Support for patching legacy modules `scandir` and `pathlib2` was removed in pyfakefs version 6.0.0. If your tests relied on these, they will break.","severity":"breaking","affected_versions":"6.0.0+"},{"fix":"Avoid using C-backed libraries for file I/O in code under test with pyfakefs, or patch them separately if possible. Consider using pure Python alternatives where applicable.","message":"pyfakefs cannot mock file system access that uses C libraries directly, rather than Python's standard `os`, `io`, or `pathlib` modules. Libraries like `lxml` fall into this category.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Prefer using pyfakefs's native patching mechanisms (e.g., `fs` fixture, `TestCase` inheritance, `Patcher`) for file system related mocks. Avoid `unittest.mock.patch` for `os`, `io`, or `pathlib` when pyfakefs is active.","message":"Using `unittest.mock.patch` on file system functions concurrently with pyfakefs can lead to incorrect behavior or conflicts, as both attempt to modify the same system modules.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid using `isinstance()` checks to differentiate file object types when using pyfakefs. Focus on duck typing or attribute checks if necessary.","message":"pyfakefs does not retain the Method Resolution Order (MRO) for its fake file objects. This means `isinstance()` checks against original file types (e.g., `io.TextIOBase`) may fail unexpectedly.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If testing multi-threaded code with file I/O, exercise caution. Consider restructuring tests to isolate file operations or to avoid concurrent access to the same fake files.","message":"pyfakefs is not guaranteed to work correctly in multi-threading environments, especially concerning concurrent write access to a single file from different threads.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}