mock-open
A better mock for file I/O
What it is and what it does
mock-open is a test utility that replaces the builtin open function with a mock that tracks file operations for unit testing. Unlike the standard mock.mock_open, it supports multiple file paths simultaneously, maintains file contents across separate open calls, and implements the full file API (read, write, seek, tell, readline, readlines, writelines). It's designed to let you test code that reads or writes files without touching the filesystem.
The package works by providing a MockOpen class that acts as a context manager and can be patched over builtins.open. You configure mock files by path, set their read_data or side effects, and then your code under test interacts with them as if they were real files. State persists between calls, so a write followed by a read on the same path returns what was written.
Use it for:
- Test code that reads configuration files without creating temporary files on disk
- Verify file write operations in unit tests by checking mock call arguments and state
- Mock multiple file paths in a single test to simulate complex I/O scenarios
- Test error handling for file operations by setting IOError side effects on specific paths
- Ensure file operations occur in the correct order or with expected arguments
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides a drop-in replacement for mock.mock_open that supports multiple files, persistent file state across open calls, and standard file operations like read, write, seek, and tell.
No. The package is abandoned (last release 2020-04-15, last commit 2020-10-10) with no maintenance or security updates. The standard library's mock.mock_open has likely evolved to address the gaps that motivated mock-open, and relying on unmaintained test infrastructure creates long-term risk. Use unittest.mock.mock_open or consider pytest-mock and related modern alternatives instead.
Install
mock-open on PyPI
pip
pip install mock-openuv
uv add mock-openpoetry
poetry add mock-openInstalling mock-open
Before you install
Installation friction is high due to source distribution format. The package is abandoned—last commit was 2020-10-10 and no releases since 2020-04-15—so expect no maintenance or security updates going forward.
License in practice
MIT license is permissive, allowing free use, modification, and distribution with minimal restrictions, suitable for both open-source and proprietary projects.
Quickstart
from unittest.mock import patch
from mock_open import MockOpen
mock_open = MockOpen()
mock_open["/path/to/file"].read_data = "test data"
with patch("builtins.open", mock_open):
with open("/path/to/file", "r") as f:
content = f.read()
Verify before relying
- Whether the package works reliably with modern Python versions (3.9+) despite being abandoned since 2020
- Whether standard library mock.mock_open has since incorporated the features that made mock-open necessary
Package facts
| License | MIT (permissive) |
| Python support | not specified |
| Install friction | high — source build required |
| Runtime dependencies | none |
| Maintenance | abandoned — 2,312 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 257,624/month — #8,439 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: mock-open-1.4.0.tar.gz
Keywords: testing, unittest, test, mock, mocking, patch, patching, stubs, fakes, doubles
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
pyfakefspyfakefs mocks the Python file system modules…
permissive · top 5,000 on PyPI
asyncmockProvides awaitable mock objects for testing…
permissive · top 15,000 on PyPI
testfixturesTestfixtures provides helpers and mock objects…
permissive · top 5,000 on PyPI
flexmockflexmock creates mock, stub, spy, and fake…
permissive · top 15,000 on PyPI
pytest-mockProvides a pytest fixture that wraps the mock…
permissive · top 1,000 on PyPI
testpathTestpath provides utilities for testing code…
permissive · top 5,000 on PyPI
requests-fileRequests-File adds file:// URL support to the…
permissive · top 1,000 on PyPI
mockmock provides a backport of Python's standard…
permissive · top 1,000 on PyPI
mocketMocket is a socket mocking framework that…
permissive · top 15,000 on PyPI
mockitoMockito is a spying and stubbing framework for…
permissive · top 5,000 on PyPI