在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.

我必须做什么才能让ellipis?据我所知,默认情况下它是禁用的.

我知道像下面的代码一样,add # 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.x相关问答推荐

如何强调您正在寻求以 pandas 数据帧的另一列为条件的差异?

我可以设置树视图层次 struct 按钮吗?

如何获取实例化 `types.GenericAlias` 的下标类?

当参数名称与 typing.Protocol 中定义的名称不同时发生 mypy 错误

为什么 return node.next 会返回整个链表?

这种类型提示有什么作用?

如何在Pandas 中按条件计算分组?

Pandas数据单调行为

在气流中运行 DAG 时出现处理信号:ttou消息

UnicodeDecodeError:utf-8编解码器无法解码位置 1 的字节 0x8b:无效的起始字节,同时读取Pandas中的 csv 文件

为什么包含类的名称不被识别为返回值函数注释?

如何判断一个字符串是否包含有效的 Python 代码

如何在 Python 中计算 cohen 的 d?

为什么`multiprocessing.Queue.get`这么慢?

无 Python 错误/错误?

从 IPython 重新加载 Python 扩展模块

如何使用已打开并使用登录凭据登录的浏览器

为什么排序列表比未排序列表大

如何使用 python http.server 运行 CGI hello world

有没有办法在多个线程中使用 asyncio.Queue ?