mock-open
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
mocklibrary 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.
Metadata
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)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mock-open-1.4.0.tar.gz | 12.1 kB | Without provenance | 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
|
Security for mock-open 1.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.