Skip to main content
Build Status Current PyPI release Download count

Garland is simple, repeatable decorator mocking.

Why?

Decorators are applied at the time the decorated function is first imported, which makes mocking them a bit more challenging.

I want to be able to be simply patch decorators - typically making them just pass-throughs to ignore their functionality - without modifying the underlying codebase. And I don’t want to worry about whether a module has already been loaded and is now unpatchable, or it needs to be patched for every test… and I don’t want to write the patching/loading/reloading code for every test.

Usage

Provided you have a function that you want to test, like so:

@my_decorator
def something_cool(*args, **kwargs):
    ...
    return some_var

With your my_decoroator decorator defined in another module, you can mock my_decorator so that you can test just the end decorated function.

In your test, apply the tinsel decorator function to the test method where you want the decorator mocked.

@garland.tinsel('utils.decorators.my_decorator', 'very_cool.module')
def test_something_cool(self):
    self.assertEqual(very_cool.module.something_cool(), "undecorated value")

Now test_something can test the return values from something_cool without the decorator potentially returning a different value, or providing a different function interface.

The tinsel decorator takes two arguments:

  1. A dotted path to the decorator function you want to mock

  2. A dotted path to the module in which your function - the one you’re testing - is declared. If you’re not importing the module like this in your tests then this will do you little good (see limits, below).

Limits

  1. You need to import modules, not named functions, to work with garland.

  2. For now this is only tested with decorator functions and function decorators

  3. The mock turns your decorator into a pass-through, assuming that this is the way you want to mock it.

  4. Only Python 3.4 and Python 2.7 are supported at this time.

License

Copyright Ben Lopatin. BSD licensed (see LICENSE).

History

0.1.0 (2015-01-15)

  • First release on PyPI.

Release files for garland 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for garland 0.2.0
File Size Uploaded
garland-0.2.0.tar.gz 12.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for garland 0.2.0
File Interpreter ABI Platform
garland-0.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 17.9 kB

Release files / garland-0.2.0.tar.gz

Download URL garland-0.2.0.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
48353de4245c6da526f968314be7d0ecfcec79e6a6813ef9426843d9dc2c12a7
BLAKE2b-256 checksum
How to use checksums
448cb5e81a0544c1e8c17322615e8b477d486cfdda1b3932e5191bd745047fff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / garland-0.2.0-py2.py3-none-any.whl

Download URL garland-0.2.0-py2.py3-none-any.whl
Size 5.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
569f116e89814cc38bdeec9172d1954844deee97020859e5a3b6b6c81178771b
BLAKE2b-256 checksum
How to use checksums
1903ec0951901babd2b3d4d07661de295c0b76f0e6896643ef0017282c2c2fbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page