首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python doctest检查特定行

python doctest检查特定行
EN

Stack Overflow用户
提问于 2013-06-10 17:51:55
回答 1查看 172关注 0票数 0

我想像这样写一个doctest:

代码语言:javascript
复制
"""
>>> checking()
some random text
some more random text
...
test is passed ##ignore all above/below lines except this one 
more and more randomness
...
finished.
"""

我真的不关心前几行或最后几行。我只担心像“测试通过了”这样的声明。我试过这样的东西

代码语言:javascript
复制
"""
>>> checking()
some random text
...
test is passed
...
finished.

"""

没有成功。这可以通过doctest来实现吗?谢谢你的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-10 18:40:00

您应该使用ELLIPSIS标志:

代码语言:javascript
复制
>>> 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函数:

代码语言:javascript
复制
>>> 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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17021102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档