Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/posener/mock-import
A helper mocking function to mask ImportErrors
https://github.com/posener/mock-import
Last synced: 3 days ago
JSON representation
A helper mocking function to mask ImportErrors
- Host: GitHub
- URL: https://github.com/posener/mock-import
- Owner: posener
- License: apache-2.0
- Created: 2016-09-16T12:31:32.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-10-29T13:36:52.000Z (about 3 years ago)
- Last Synced: 2024-10-04T13:40:11.140Z (about 1 month ago)
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 17
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
mock_import
===========.. image:: https://travis-ci.org/posener/mock-import.svg?branch=master
:target: https://travis-ci.org/posener/mock-import.. image:: https://badge.fury.io/py/mock-import.svg
:target: https://pypi.python.org/pypi/mock-import.. image:: https://codecov.io/gh/posener/mock-import/branch/master/graph/badge.svg
:target: https://codecov.io/gh/posener/mock-importA helper mocking function to mask ``ImportError`` s on a scoped code.
Failed imports will be ignored, unless specified by the *do_not_mock* argument.Installation
------------Using pip: ``pip install mock-import``
Usage
-----Import:
>>> from mock_import import mock_importMocking import for a code block:
>>> with mock_import():
... import no_such_module # Won't raise ImportError
... no_such_module.no_such_function() # Won't raise AttributeErrorMocking import as a decorator:
>>> @mock_import()
... def method():
... import no_such_module # Won't raise ImportError
... no_such_module.no_such_function() # Won't raise AttributeError>>> import no_such_module # raises ImportError
Making an exception:
>>> with mock_import(do_not_mock='no_such_module'):
... import no_such_other_module # Won't raise ImportError
... import no_such_module # Will raise ImportError>>> with mock_import(do_not_mock=['nsm1', 'nsm2']):
... import nsm # Won't raise ImportError
... import nsm1 # Will raise ImportError
... import nsm2 # Will raise ImportError