Skip to main content

A better mock for file I/O

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mock-open-1.4.0.tar.gz (12.1 kB view details)

Uploaded Source

File details

Details for the file mock-open-1.4.0.tar.gz.

File metadata

  • Download URL: mock-open-1.4.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for mock-open-1.4.0.tar.gz
Algorithm Hash digest
SHA256 c3ecb6b8c32a5899a4f5bf4495083b598b520c698bba00e1ce2ace6e9c239100
MD5 25f39b17675491dfc4a20b6c54380215
BLAKE2b-256 9a02cef85a80ff6d3092a458448c46816656d1c532afd45aeeeb8f50a84aed35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page