Doctests and decorated methods don't get along

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Feb 6 06:48:56 EST 2010


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?



-- 
Steven



More information about the Python-list mailing list