skillfed

mock-open

A better mock for file I/O

mock-open v1.4.0 257.6K downloads/30d#8,439 on PyPI7
Permissive license MIT Abandoned released

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-open

uv

uv add mock-open

poetry

poetry add mock-open

Installing 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

Development Status :: 5 - Production/StableEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 2.7Programming Language :: Python :: 3Topic :: Software DevelopmentTopic :: Software Development :: LibrariesTopic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: Testing

Tags

mock file operationsfile I/O testingmock open replacementunit test file mockingfake file systemmock multiple filespersistent mock files
abandonedtesting-utility

More Software Development packages