tags:

views:

309

answers:

1

Hi!

I'd like to write a doctest like this:

"""
>>> print a.string()
          foo : a
          bar : b
         date : <I don't care about the date output>
          baz : c
"""

Is there any way to do this? I think it would make more sense to switch to unittest, but I'm curious whether it's possible to specify a range of output that shouldn't be matched for the test in doctest.

Thanks!

+5  A: 

With doctest.ELLIPSIS, you can use ... to mean "match any string here". You can set doctest options with a doctest directive, to make it active for just one test case: one example in the online docs is:

>>> print range(20) # doctest:+ELLIPSIS
[0, 1, ..., 18, 19]

If you want a doctest option to be active throughout, you can pass it as the optionflags= argument to whatever doctest functions you use, e.g. doctest.testfile. (You can pass multiple option flags there by using the | operator to bit-or them).

Alex Martelli
Thanks for the answer!
cjb
But how can we ignore the whole line?
t0ster
@t0ster, just put `...` as the whole of the "expected output" (with `doctest.ELLIPSIS` set, of course), and doctest will accept any content on that output line, i.e. "ignore the whole line" of output.
Alex Martelli