doctest
requires exceptions to look a certain way. From the docs:
Each line of the traceback stack (if present) must be indented further than the first line of the example, or start with a non-alphanumeric character. The first line following the traceback header indented the same and starting with an alphanumeric is taken to be the start of the exception detail.
(added bold)
This means if you make the ELLIPSIS_MARKER
start with an alphanumeric, it'll work properly. Here's an example using re.error
:
def test():
"""
>>> import doctest
>>> doctest.ELLIPSIS_MARKER = 'MODULE.'
>>> import re
>>> raise re.error(None) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
MODULE.error: None
"""
By the way:
Note that tracebacks are treated very specially. In particular, in the rewritten example, the use of ...
is independent of doctest's ELLIPSIS
option. The ellipsis in that example could be left out, or could just as well be three (or three hundred) commas or digits, or an indented transcript of a Monty Python skit.
For context, here's an example with no exception that uses two ellipses:
def test():
r"""
>>> print('foo\nbar\nbaz') # doctest: +ELLIPSIS
foo
...
...
"""
That said, IGNORE_EXCEPTION_DETAIL
may be a better solution. (I just learned about it myself.)
When specified, an example that expects an exception passes if an exception of the expected type is raised, even if the exception detail does not match. For example, an example expecting ValueError: 42
will pass if the actual exception raised is ValueError: 3*14
, but will fail, e.g., if TypeError
is raised.
It will also ignore the module name used in Python 3 doctest reports.
(added bold)
For example:
def test():
"""
>>> import re
>>> raise re.error(None) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
error: foobar
"""
Note that both the exception module and exception details are ignored in this example. That's on purpose, to show a side-effect of this solution.