The doctest of the following (nonsense) Python module fails:
"""
>>> L = []
>>> if True:
... append_to(L) # XXX
>>> L
[1]
"""
def append_to(L):
L.append(1)
class A(object):
pass
return A()
import doctest; doctest.testmod()
This is because the output after the line marked XXX is <__main__.A object at ...>
(which is returned by append_to
). Of course, I could put this output directly after the line marked XXX but in my case this would distract the reader from what shall be actually tested, namely the side effect of the function append_to
. So how can I suppress that output or how can I ignore it. I tried it with:
"""
>>> L = []
>>> if True:
... append_to(L) # doctest: +ELLIPSIS
...
>>> L
[1]
"""
def append_to(L):
L.append(1)
class A(object):
pass
return A()
import doctest; doctest.testmod()
However, this yields a ValueError: line 4 of the docstring for __main__ has inconsistent leading whitespace: ' ...'
.
What I don't want to do is to change the line append_to(L)
to something like _ = append_to(L)
which would suppress the output, because the doctest is for documentation purposes and to show the reader how the module is supposed to be used.
(In the case being documented, append_to
should be used statement-like and not like a function. Writing _ = append_to(L)
would deviate the reader from this.)