python - 值错误 : wrapper loop when unwrapping

标签 python doctest

Python3 测试用例 (doctests) 在我的示例代码中失败了。但同样适用于 Python2。

测试.py

class Test(object):
    def __init__(self, a=0):
        self.a = a

    def __getattr__(self, attr):
        return Test(a=str(self.a) + attr)

tst.py

from test import Test

t = Test()

运行测试用例:python3 -m doctest -v tst.py

错误:

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.6/doctest.py", line 2787, in <module>
    sys.exit(_test())
  File "/usr/lib/python3.6/doctest.py", line 2777, in _test
    failures, _ = testmod(m, verbose=verbose, optionflags=options)
  File "/usr/lib/python3.6/doctest.py", line 1950, in testmod
    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
  File "/usr/lib/python3.6/doctest.py", line 933, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File "/usr/lib/python3.6/doctest.py", line 992, in _find
    if ((inspect.isroutine(inspect.unwrap(val))
  File "/usr/lib/python3.6/inspect.py", line 513, in unwrap
    raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
ValueError: wrapper loop when unwrapping <test.Test object at 0x7f6e80028550>

任何人都可以帮助解决这个错误。

谢谢。

最佳答案

对于 unittest.mock 尝试将项目导入为

from unittest import mock

代替

from unittest.mock import patch

这对我来说解决了这个错误。

关于python - 值错误 : wrapper loop when unwrapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49271586/

相关文章:

python - 如何将此 Python 代码转换为 C++

python - Python doctest中的多行字符串输出

python - 如何在 docutils 中处理空值

python - pandas 数据框使用列和特定行进行转换

python - 阻止 Matplotlib Jupyter notebooks 显示带有动画的情节

python - 从python获取java版本号

rust - 推断调用包的名称以在过程宏中填充一个 doctest

python - 文档测试 : How to suppress/ignore output?

python - 为什么 py.test 向后运行模块 doctests?

Python:输出列表= x输入列表的最大数量