Doctests and decorated methods don't get along
Diez B. Roggisch
deets at nospam.web.de
Tue Feb 9 03:40:16 EST 2010
Am 06.02.10 12:48, schrieb Steven D'Aprano:
> It seems that doctest doesn't discover or execute doctests in methods
> which have been decorated.
>
>
> Here is my test file:
>
> # ======
> class MyStaticMethod(object):
> """Emulate built-in staticmethod descriptor."""
> def __init__(self, f):
> self.f = f
> def __get__(self, obj, objtype=None):
> return self.f
>
> class Test(object):
> def method1(self):
> """Doc string
>
> >>> print 'x'
> x
> """
>
> @staticmethod
> def method2():
> """Doc string
>
> >>> print 'y'
> y
> """
>
> @MyStaticMethod
> def method3():
> """Doc string
>
> >>> print '***' # This test should fail.
> z
> """
>
> if __name__ == '__main__':
> import doctest
> doctest.testmod(verbose=True)
> # ======
>
>
> and here is the output using Python 2.6.4:
>
> [steve at sylar python]$ python2.6 test_doctests.py
> Trying:
> print 'x'
> Expecting:
> x
> ok
> Trying:
> print 'y'
> Expecting:
> y
> ok
> 5 items had no tests:
> __main__
> __main__.MyStaticMethod
> __main__.MyStaticMethod.__get__
> __main__.MyStaticMethod.__init__
> __main__.Test
> 2 items passed all tests:
> 1 tests in __main__.Test.method1
> 1 tests in __main__.Test.method2
> 2 tests in 7 items.
> 2 passed and 0 failed.
> Test passed.
>
>
> It doesn't even see method3, let alone run the tests.
>
>
> This looks like bug to me. Have I missed anything?
>
> Any work arounds?
Use functools.wraps to preserve some essential parts of the function
declaration.
def my_static(f):
@wraps(f)
def _d(*args, **kwargs):
return f(*args, **kwargs)
return _d
Diez
More information about the Python-list
mailing list