Skipping decorators in unit tests

Ned Batchelder ned at nedbatchelder.com
Thu Oct 10 19:44:45 EDT 2013


On 10/10/13 6:12 PM, Cameron Simpson wrote:
> On 10Oct2013 07:00, Gilles Lenfant <gilles.lenfant at gmail.com> wrote:
>> (explaining the title) : my app has functions and methods (and
>> maybe classes in the future) that are decorated by decorators
>> provided by the standard library or 3rd party packages.
>>
>> But I need to test "undecorated" functions and methods in my unit tests, preferably without adding "special stuffs" in my target tested modules.
>>
>> Can someone point out good practices or dedicated tools that "remove temporarily" the decorations.
>> I pasted a small example of what I heed at http://pastebin.com/20CmHQ7Y
> 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().
>
> And so forth.
>
> Cheers,

I have to admit I'm having a hard time understanding why you'd need to 
test the undecorated functions.  After all, the undecorated functions 
aren't available to anyone.  All that matters is how they behave with 
the decorators.

But my imagination is weak: do you mind explaining more about what the 
functions do, what the decorators do, and why you need to test the 
undecorated functions?  I'll learn something, and with more information, 
we might be able to find a better solution.

--Ned.




More information about the Python-list mailing list