mock-open


Namemock-open JSON
Version 1.4.0 PyPI version JSON
download
home_pagehttp://github.com/nivbend/mock-open
SummaryA better mock for file I/O
upload_time2020-04-15 15:26:51
maintainer
docs_urlNone
authorNiv Ben-David
requires_python
licenseMIT
keywords testing unittest test mock mocking patch patching stubs fakes doubles
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            mock-open
=========
[![PyPI version](https://badge.fury.io/py/mock-open.svg)](https://pypi.python.org/pypi/mock-open/)
[![Build Status](https://travis-ci.org/nivbend/mock-open.svg?branch=master)](https://travis-ci.org/nivbend/mock-open)
[![GitHub license](https://img.shields.io/github/license/nivbend/mock-open.svg)](https://github.com/nivbend/mock-open/blob/master/LICENSE)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://GitHub.com/nivbend/mock-open/graphs/commit-activity)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

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`][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:
   ```python
   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:
   ```python
   mock_open = MockOpen()
   mock_open.return_value.write.side_effect = IOError()
   ```

 * Persistent file contents between calls to `open`:
   ```python
   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][CPython] as part of its
test suite. The original tests are licensed under the [PSF license agreement][PSF License] and are
copyright of the Python Software Foundation.

[mock-open]: http://docs.python.org/library/unittest.mock.html#mock-open
[CPython]: https://github.com/python/cpython
[PSF License]: https://docs.python.org/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python
            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/nivbend/mock-open",
    "name": "mock-open",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "testing unittest test mock mocking patch patching stubs fakes doubles",
    "author": "Niv Ben-David",
    "author_email": "nivbend@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9a/02/cef85a80ff6d3092a458448c46816656d1c532afd45aeeeb8f50a84aed35/mock-open-1.4.0.tar.gz",
    "platform": "",
    "description": "mock-open\n=========\n[![PyPI version](https://badge.fury.io/py/mock-open.svg)](https://pypi.python.org/pypi/mock-open/)\n[![Build Status](https://travis-ci.org/nivbend/mock-open.svg?branch=master)](https://travis-ci.org/nivbend/mock-open)\n[![GitHub license](https://img.shields.io/github/license/nivbend/mock-open.svg)](https://github.com/nivbend/mock-open/blob/master/LICENSE)\n[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://GitHub.com/nivbend/mock-open/graphs/commit-activity)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n\nA better mock for file I/O.\n\nInstall\n-------\n```\n$ pip install mock-open\n```\n\nclass `MockOpen`\n----------------\nThe `MockOpen` class should work as a stand-in replacement for [`mock.mock_open`][mock-open] with\nsome added features (though it tries to conform to how the builtin `open` works where the two\ndiffer):\n * Multiple file support, including a mapping-like access to file mocks by path:\n   ```python\n   from mock_open import MockOpen\n   mock_open = MockOpen()\n   mock_open[\"/path/to/file\"].read_data = \"Data from a fake file-like object\"\n   mock_open[\"/path/to/bad_file\"].side_effect = IOError()\n   ```\n\n   You can also configure behavior via the regular `mock` library API:\n   ```python\n   mock_open = MockOpen()\n   mock_open.return_value.write.side_effect = IOError()\n   ```\n\n * Persistent file contents between calls to `open`:\n   ```python\n   with patch(\"builtins.open\", MockOpen()):\n       with open(\"/path/to/file\", \"w\") as handle:\n           handle.write(\"Some text\")\n\n       with open(\"/path/to/file\", \"r\") as handle:\n           assert \"Some text\" == handle.read()\n   ```\n\n * All the regular file operations: `read`, `readline`, `readlines`, `write`, `writelines`, `seek`,\n   `tell`.\n\nAcknowledgements\n----------------\nThis library uses modified versions of tests from the [CPython source code][CPython] as part of its\ntest suite. The original tests are licensed under the [PSF license agreement][PSF License] and are\ncopyright of the Python Software Foundation.\n\n[mock-open]: http://docs.python.org/library/unittest.mock.html#mock-open\n[CPython]: https://github.com/python/cpython\n[PSF License]: https://docs.python.org/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A better mock for file I/O",
    "version": "1.4.0",
    "split_keywords": [
        "testing",
        "unittest",
        "test",
        "mock",
        "mocking",
        "patch",
        "patching",
        "stubs",
        "fakes",
        "doubles"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9a02cef85a80ff6d3092a458448c46816656d1c532afd45aeeeb8f50a84aed35",
                "md5": "25f39b17675491dfc4a20b6c54380215",
                "sha256": "c3ecb6b8c32a5899a4f5bf4495083b598b520c698bba00e1ce2ace6e9c239100"
            },
            "downloads": -1,
            "filename": "mock-open-1.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "25f39b17675491dfc4a20b6c54380215",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 12127,
            "upload_time": "2020-04-15T15:26:51",
            "upload_time_iso_8601": "2020-04-15T15:26:51.234780Z",
            "url": "https://files.pythonhosted.org/packages/9a/02/cef85a80ff6d3092a458448c46816656d1c532afd45aeeeb8f50a84aed35/mock-open-1.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-04-15 15:26:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "nivbend",
    "github_project": "mock-open",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "mock-open"
}
        
Elapsed time: 0.03536s