سؤال

I'd like to write a doctest like this:

"""
>>> checking()
some random text
some more random text
...
test is passed ##ignore all above/below lines except this one 
more and more randomness
...
finished.
"""

I don't really care about first few lines or last few lines . I'm worried only about statement like "test is passed" . I tried something like

"""
>>> checking()
some random text
...
test is passed
...
finished.

"""

without success. Is this possible with doctest? Thanks for any help

هل كانت مفيدة؟

المحلول

You should use the ELLIPSIS flag:

>>> def checking():
...     """
...     >>> checking()  #doctest: +ELLIPSIS
...     header
...     ...
...     test is passed
...     ...
...     footer
...     """
...     print("header\nrandom\nlines\ntest is passed\nother\nrandom lines\nfooter")
>>> doctest.testmod(verbose=True)
Trying:
    checking()  #doctest: +ELLIPSIS
Expecting:
    header
    ...
    test is passed
    ...
    footer
ok
1 items had no tests:
    __main__
1 items passed all tests:
   1 tests in __main__.checking
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
TestResults(failed=0, attempted=1)

The ... can be used only in exception tracebacks without the ELLIPSIS option.

If you don't want to use the directives in the docstrings you can pass the optionflags parameter to the doctest functions:

>>> checking.__doc__ = ''.join(checking.__doc__.split('#doctest: +ELLIPSIS'))
>>> print checking.__doc__

    >>> checking()  
    header
    ...
    test is passed
    ...
    footer

>>> doctest.testmod(optionflags=doctest.ELLIPSIS)
TestResults(failed=0, attempted=2)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top