Skip to main content

mock-open

PyPI version Build Status GitHub license Maintenance PRs Welcome

A better mock for file I/O.

Install

$ pip install mock-open

class MockOpen

The MockOpen class should work as a stand-in replacement for mock.mock_open with some added features (though it tries to conform to how the builtin open works where the two differ):

  • Multiple file support, including a mapping-like access to file mocks by path:

    from mock_open import MockOpen
    mock_open = MockOpen()
    mock_open["/path/to/file"].read_data = "Data from a fake file-like object"
    mock_open["/path/to/bad_file"].side_effect = IOError()
    

    You can also configure behavior via the regular mock library API:

    mock_open = MockOpen()
    mock_open.return_value.write.side_effect = IOError()
    
  • Persistent file contents between calls to open:

    with patch("builtins.open", MockOpen()):
        with open("/path/to/file", "w") as handle:
            handle.write("Some text")
    
        with open("/path/to/file", "r") as handle:
            assert "Some text" == handle.read()
    
  • All the regular file operations: read, readline, readlines, write, writelines, seek, tell.

Acknowledgements

This library uses modified versions of tests from the CPython source code as part of its test suite. The original tests are licensed under the PSF license agreement and are copyright of the Python Software Foundation.

Release files for mock-open 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mock-open 1.4.0
File Size Uploaded
mock-open-1.4.0.tar.gz 12.1 kB Details

Release files / mock-open-1.4.0.tar.gz

Download URL mock-open-1.4.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c3ecb6b8c32a5899a4f5bf4495083b598b520c698bba00e1ce2ace6e9c239100
BLAKE2b-256 checksum
How to use checksums
9a02cef85a80ff6d3092a458448c46816656d1c532afd45aeeeb8f50a84aed35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

1.4.0 This release

1 release file

1.3.2

1 release file

1.3.1

1 release file

1.3.0

1.2.2

1 release file

1.2.1

1 release file

1.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page