Skipping decorators in unit tests
Ethan Furman
ethan at stoneleaf.us
Fri Oct 11 13:51:01 EDT 2013
On 10/10/2013 08:13 PM, Cameron Simpson wrote:
> On 11Oct2013 02:55, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>> On Fri, 11 Oct 2013 09:12:38 +1100, Cameron Simpson wrote:
>>> Speaking for myself, I would be include to recast this code:
>>>
>>> @absolutize
>>> def addition(a, b):
>>> return a + b
>>>
>>> into:
>>>
>>> def _addition(a, b):
>>> return a + b
>>> addition = absolutize(_addition)
>>>
>>> Then you can unit test both _addition() and addition().
>>
>> *shudders*
>> Ew ew ew ew.
>
> Care to provide some technical discourse here? Aside from losing the neat
> and evocative @decorator syntax, the above is simple and overt.
And completely dismisses the whole point of adding @decorator to the
language: easy to use, easy to see == folks will actually use it.
>> I would much rather do something like this:
>>
>> def undecorate(f):
>> """Return the undecorated inner function from function f."""
>> return f.func_closure[0].cell_contents
>
> Whereas this feels like black magic. Is this portable to any decorated
> function? If so, I'd have hoped it was in the stdlib. If not: black magic.
Probably black magic. But you can go with the decorator.wrapped route;
after all, you're testing your own stuff so you should have control of
your own decorators (okay, you may have to adapt a few others ;) .
--
~Ethan~
More information about the Python-list
mailing list