Skip to main content

A decorator designed to make testing optional dependencies painless.

Project description

A decorator designed to make testing optional dependencies painless.

It’s common for libraries to have optional dependencies where the code has to detect if they are installed, using a pattern like this:

try:
    import something
else:
    import something_else as something

That way something is always defined and the rest of the codebase does not need to care. But how do we test both branches under tests?

With fail_importing, you can just slap a decorator onto your test and the code being tested will act as if the dependency isn’t installed.

@fail_importing("something")
def test_something_else():
    ...

When the test finishes executing, the name can be imported as normal. It can be applied to normal functions, nested functions, relative imports, and even generators!

full_match is used to determine if the path matches or not, so you can use any regex matching you need. Note that relative imports are resolved to absolute imports before the check is made.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fail_importing-1.0.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

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