to Doctest as SystemExit is to Python

Peter Otten __peter__ at web.de
Fri Nov 10 03:39:39 EST 2006


p.lavarre at ieee.org wrote:

> Can I somehow tell doctest that it's time to quit?

Hit Ctrl-C. Or raise a KeyboardInterrupt:

import sys

class ExitDoctest(KeyboardInterrupt):
    pass

def f(what):
    """
    >>> f("alpha")
    'alpha'
    >>> f("e")
    'e'
    >>> f("x")
    'x'
    >>> f("X")
    'X'
    >>> f("beta")
    'beta'
    """
    if what == "e":
        raise Exception
    elif what == "x":
        sys.exit()
    elif what == "X":
        raise ExitDoctest("You're in trouble")
    return what

if __name__ == "__main__":
    import doctest
    try:
        doctest.testmod()
    except ExitDoctest, e:
        print >> sys.stderr, e

Peter



More information about the Python-list mailing list