Skip to content Skip to sidebar Skip to footer

Is There Way To Only Perform The Doctests, Ignoring Print Function Calls?

Hypothetically speaking, my function returns a value and has lot of print statements (maybe 100 or more). Is there a way to run doctest such that all the other printing work can b

Solution 1:

doctest uses stdout, notstderr, to show messages from any failing tests. Therefore you cannot patch out stdout as this answer originally suggested - this will suppress your print calls and any messages from doctest.


One option is to define functions that print with an additional verbose parameter, so that you can suppress this when necessary.

deffoo(verbose=True):
    """Does whatever.

        >>> foo(verbose=False)

    """if verbose:
        print('Hello world')

Although you have to change the functions, this also gives you useful options when not testing.


Another is to explicitly supply the appropriate print function to the functions that use it, allowing you to pass a NOOP at runtime:

defbar(print=print):
    """Does whatever.

        >>> bar(print=lambda *args, **kwargs: None)

    """print('Hello world')

This also requires changes to function definitions, but at least avoids changes in the bodies of those functions.


A third option is to patch out print for the whole module under test, for example:

defbaz():
    """Does whatever.

        >>> baz()

    """print('Hello world')

if __name__ == '__main__':

    import doctest

    print = lambda *args, **kwargs: None

    doctest.testmod()

Note that this affects the outputs that doctest sees, too, so you don't include any of the print output in the docstring (I assume this is good news!) It won't work with python -m doctest mymodule.py, though.

Solution 2:

in addition to jonrsharpe's excellent answer, there is one more way, that does work with the python3 -m doctest module.py construct.

#!/usr/bin/python3 -OO'''
Some ideas for adding additional verbosity during doctest, and for
reducing verbosity and startup delays during doctest or pydoc.
'''from __future__ import print_function  # for compatibility with python2import sys, os, logging
logging.basicConfig(level = logging.DEBUG if__debug__else logging.INFO)
COMMAND = os.path.splitext(os.path.basename(sys.argv[0]))[0]
if COMMAND in ['doctest', 'pydoc']:
    NONDOCTESTPRINT = lambda *args, **kwargs: None
    DOCTESTDEBUG = logging.debug
else:
    NONDOCTESTPRINT = print
    DOCTESTDEBUG = lambda *args, **kwargs: None# You can also use this `else` block to import things not needed during# doctest, especially slow-loading modules like `requests`,# or to do some other verbose or slow initialization.deftest(string):
    '''
    print string after lead-in

    >>> test("I'm in love with you!")
    Listen!
    I'm in love with you!
    '''
    DOCTESTDEBUG("If this works, you shouldn't see anything but this")
    print('Listen!')
    NONDOCTESTPRINT('Do you want to know a secret?')
    NONDOCTESTPRINT('Do you promise not to tell? Whoa, oh...')
    NONDOCTESTPRINT('Closer...')
    NONDOCTESTPRINT('Let me whisper in your ear...')
    NONDOCTESTPRINT('Say the words you long to hear...')
    print(string)

if __name__ == '__main__':
    test(' '.join(sys.argv[1:]) or'Taxation is theft.')

Here is what the output looks like, depending on how it's called.

jcomeau@aspire:/tmp$ python3 -m doctest doctesttest.py 
DEBUG:root:If this works, you shouldn't see anything but this
jcomeau@aspire:/tmp$ python3 doctesttest.py This is a test!
Listen!
Do you want to know a secret?
Do you promise notto tell? Whoa, oh...
Closer...
Letme whisper in your ear...
Say the words you longto hear...
This is a test!

and for pydoc doctesttest:

pydoc output of above script

Post a Comment for "Is There Way To Only Perform The Doctests, Ignoring Print Function Calls?"