Evolving doctests for changing output format

Raymond Hettinger python at rcn.com
Thu Jan 11 03:58:35 CET 2007


[nriley at gmail.com]
> What I'd like is if I could get doctest to take my tests, and
> substitute the obtained output for the provided output.

There's currently no support for auto-updating doctests.  I think it
would make for a good feature request.

In the meantime, it may not be difficult to roll your own by converting
the current expected/got output into a diff and then applying it to the
original source code being doctested.

Start with a regular expression like this:

pat = re.compile(r'''
File.+,\sline\s(\d+),.+\n
Failed\sexample:\n((?:.*?\n)*?)
Expected:\n((?:.*?\n)*?)
Got:\n((?:.*?\n)*?)
\*{70,70}
''', (re.MULTILINE | re.VERBOSE))

Then, build-up a diff in your preferred format.  Perhaps like this:

cumadj = 0
for lineno, example, expected, got in pat.findall(source):

    len_example, len_expected, len_got = example.count('\n'),
expected.count('\n') , got.count('\n')
    start = int(lineno) + len_example
    lead = str(start)
    if len_expected !=1:
        lead += ',' + str(start+len_expected-1)
    lead += 'c' + str(start + cumadj)
    if len_got != 1:
        lead += ',' + str(start+cumadj+len_got-1)

    print lead
    for old in expected.splitlines(False):
        print '<' + old
    print '---'
    for new in got.splitlines(False):
        print '>' + new

    cumadj += len_got - len_expected 



Raymond




More information about the Python-list mailing list