How enable ellipsis when calling Python doctest?
You can enable options for an individual example like this:
'''
>>> 'foobarbaz' # doctest: +ELLIPSIS
'foo...baz'
'''
The doctest
directives documentation is hard to understand because the actual directives seem to be parsed away and aren't visible. There is an open bug report for this. In the meantime you can view the raw documentation source instead.
Since Python 3.4, you can pass the option with the -o
flag:
$ python -m doctest -o=ELLIPSIS foo.py
Source: https://docs.python.org/3/library/doctest.html#option-flags
You can pass in optionflags
to the testmod
method, but this requires you to run the module itself instead of the doctest
module:
def foo():
"""
>>> foo()
hello ...
"""
print("hello world")
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True, optionflags=doctest.ELLIPSIS)
Output:
$ python foo.py
Trying:
foo()
Expecting:
hello ...
ok
1 items had no tests:
__main__
1 items passed all tests:
1 tests in __main__.foo
1 tests in 2 items.
1 passed and 0 failed.
Test passed.