在 Python (3.3.2) doctest 中,省略号 (...
)可以匹配任何字符串。因此,对于下面的代码
def foo():
"""
>>> foo()
hello ...
"""
print("hello world")
当运行doctest时,它不应该出现任何错误。但是
$ python -m doctest foo.py
**********************************************************************
File "./foo.py", line 3, in foo.foo
Failed example:
foo()
Expected:
hello ...
Got:
hello world
**********************************************************************
1 items had failures:
1 of 1 in foo.foo
***Test Failed*** 1 failures.
我必须做什么才能启用省略号?据我所知,默认情况下它是禁用的。
我知道添加 # doctest: +ELLIPSIS
在下面的代码中,可以解决这个问题,但我喜欢为所有测试启用省略号。
def foo():
"""
>>> foo() # doctest: +ELLIPSIS
hello ...
"""
print("hello world")
你可以传入 optionflags
至 testmod
方法,但这需要您运行模块本身,而不是运行 doctest
模块:
def foo():
"""
>>> foo()
hello ...
"""
print("hello world")
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True, optionflags=doctest.ELLIPSIS)
输出。
$ 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.
自Python 3.4以来,你可以通过带有 -o
旗帜
$ python -m doctest -o=ELLIPSIS foo.py
资料来源:Http:/docs.org3librarydoctest.html#option-flags。https:/docs.python.org3librarydoctest.html#option-flags。