Python decorator mocking.
Garland is simple, repeatable decorator mocking.
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.
Provided you have a function that you want to test, like so:
@my_decorator def something_cool(*args, **kwargs): ... return some_var
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")
test_something can test the return values from
the decorator potentially returning a different value, or providing a different
tinsel decorator takes two arguments:
Copyright Ben Lopatin. BSD licensed (see
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|garland-0.2.0-py2.py3-none-any.whl (5.1 kB) Copy SHA256 Checksum SHA256||3.4||Wheel||Jan 15, 2015|
|garland-0.2.0.tar.gz (12.8 kB) Copy SHA256 Checksum SHA256||–||Source||Jan 15, 2015|