doctest and decorators

Daniel Larsson daniel.j.larsson at gmail.com
Tue Sep 4 17:01:17 EDT 2007


On 9/4/07, Ferenczi Viktor <python at cx.hu> wrote:
>
> > I assume this is a FAQ, but I couldn't find much helpful information
> > googling. I'm having trouble with doctest skipping my functions, if I'm
> > using decorators (that are defined in a separate module). If I'm
> > understanding what is happening correctly, it's because doctest checks
> if
> > the function's func_global attribute is the same as the module's
> __dict__
> > attribute. The decorator in question returns a wrapping function,
> though,
> > so this check fails.
> > What are some solutions to this? I can define __test__, but it seems
> rather
> > cumbersome.
>
> Please take a look at the documentation of the functools standard module
> in
> the Python manual, especially the wraps decorator. It could probably help.
>


Hmm, not really.

# decorator.py
import functools

def simplelog(f):
    @functools.wraps
    def new_f(*args, **kwds):
        print "Wrapper calling func"
        return f(*args, **kwds)
    return new_f

# test.py
from decorator import simplelog

@simplelog
def test():
    """
    >>> test()
    'works!'
    """
    return "works!"

if __name__ == '__main__':
    import doctest
    doctest.testmod()

$ python test.py -v
1 items had no tests:
    __main__
0 tests in 1 items.
0 passed and 0 failed.
Test passed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070904/e7819280/attachment.html>


More information about the Python-list mailing list