Skip to main content

A helper mocking function to mask ImportErrors

Project description

https://travis-ci.org/posener/mock-import.svg?branch=master https://badge.fury.io/py/mock-import.svg

A 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_import
Mocking 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 AttributeError
Mocking 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

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-import-0.0.3.tar.gz (1.8 kB view details)

Uploaded Source

File details

Details for the file mock-import-0.0.3.tar.gz.

File metadata

  • Download URL: mock-import-0.0.3.tar.gz
  • Upload date:
  • Size: 1.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mock-import-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a29586f195c872d3fdcf1184230ae248c6432d2b453f7052c6f9a21bf7073d22
MD5 0f37fc6b6b8bfb901f05ee94db9c5978
BLAKE2b-256 d92ad439982d958cb60c5f4ae6ef10709c195ebfe5e20cb15c383708d46440a2

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