doctest omits decorator functions / decorated def – a simple workaround

Apparently there is a known issue with doctests, 
in which tests in functions using externally 
defined decorators are ignored by doctest.
Here's a simple workaround.

 

@aspect.processedby(aspect.tracing_processor)
def aa():
    ''' Returns None
 
    -- doctests ----
 
    >>> aa()
    True
 
    '''
    return False
 
def all_tests():
    ''' Returns None
 
    -- doctests ----
 
    >>> aa()
    True
    >>> aa()
    False
 
    '''
 
    return False

 

In the above snippet, my aa function is decorated and as expected, doctest ignores the tests within the docstring.

However, the all_tests function is not decorated and doctest picks it up and runs the tests.

**********************************************************************
File "C:\1\python\test_doctest.py", line 31, in __main__.all_tests
Failed example:
    aa()
Expected:
    True
Got:
    False
**********************************************************************
1 items had failures:
   1 of   2 in __main__.all_tests
***Test Failed*** 1 failures.
completed doctest

 

This is a simple workaround (and solution) and the obvious disadvantage is that the tests are further away from the tested function.

That is not altogether too bad, considering that if you switch to other unit testing methods like nose, py.test, you would have to place your tests separately anyway.

Custom Search