0

我想写一个这样的doctest:

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

我真的不在乎前几行或最后几行。我只担心“测试通过”之类的声明。我尝试了类似的东西

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

"""

没有成功。这可能与doctest吗?谢谢你的帮助

4

1 回答 1

1

您应该使用该ELLIPSIS标志:

>>> 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)

...只能在没有该选项的异常回溯中使用ELLIPSIS

如果您不想在文档字符串中使用指令,您可以将optionflags参数传递给doctest函数:

>>> 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)
于 2013-06-10T10:39:59.717 回答