Evolving doctests for changing output format

Discussion in 'Python' started by nriley@gmail.com, Jan 9, 2007.

  1. Guest

    Hi,

    I've got a number of doctests which rely on a certain output format,
    and since I wrote the tests I've changed the output format. Now,
    almost all the tests fail.

    What I'd like is if I could get doctest to take my tests, and
    substitute the obtained output for the provided output. Then, I could
    in this case just replace the doctest file with the generated file, or
    in general be able to run it through a file comparison tool so I could
    examine the differences.

    If it's not clear from the above, here's an example, assuming for the
    sake of simplicity that Python somehow changed its default output base
    between runs.

    INPUT:
    >> 10 + 10

    20

    OUTPUT:
    >> 10 + 10

    0x14

    Is it possible to use doctest in such a "run-only" mode, or script the
    above using its API? I read the documentation several times but just
    got further confused (sorry!).

    Thanks,

    --Nicholas
    , Jan 9, 2007
    #1
    1. Advertising

  2. []
    > 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
    Raymond Hettinger, Jan 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWtsYXBw?=

    Evolving Web App Standards and ASP.NET

    =?Utf-8?B?bWtsYXBw?=, Jul 19, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    415
    Mr. Dot Net
    Jul 20, 2004
  2. Bjorn Tillenius

    unicode in doctests

    Bjorn Tillenius, Nov 29, 2004, in forum: Python
    Replies:
    0
    Views:
    254
    Bjorn Tillenius
    Nov 29, 2004
  3. Thomas Heller

    Running doctests with unittest

    Thomas Heller, Mar 9, 2005, in forum: Python
    Replies:
    1
    Views:
    455
    Jim Sizelove
    Mar 9, 2005
  4. sam
    Replies:
    1
    Views:
    210
    Steven D'Aprano
    Nov 5, 2006
  5. jcervidae
    Replies:
    1
    Views:
    274
    Diez B. Roggisch
    May 18, 2009
Loading...

Share This Page