【问题标题】:Why could doctests raise a NameError when run with Sphinx's `make doctest`?为什么 doctest 在使用 Sphinx 的“make doctest”运行时会引发 NameError?
【发布时间】:2014-07-05 19:49:34
【问题描述】:

我有一个简单的function with a doctest,当使用 Sphinx 的make doctest 运行时,会出现以下错误:

File "scheemey.rst", line ?, in default
Failed example:
    verify_balanced('asdf (foo [bar] [[baz], {}, ()]')
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1315, in __run
        compileflags, 1) in test.globs
      File "<doctest default[0]>", line 1, in <module>
        verify_balanced('asdf (foo [bar] [[baz], {}, ()]')
    NameError: name 'verify_balanced' is not defined

这可能是什么原因造成的?

【问题讨论】:

    标签: python python-sphinx doctest


    【解决方案1】:

    如果带有测试功能的模块没有正确导入,我可以重现问题中的错误。

    要使其工作,您可以使用testsetup 指令:

    .. testsetup:: 
    
       from yourmodule import verify_balanced
    
    >>> verify_balanced('asdf (foo) [bar] [[baz], {}, ()]')
    >>> verify_balanced('asdf (foo [bar] [[baz], {}, ()]')
    5
    

    请注意,doctest 会忽略 None 返回值(请参阅 Python doctests: test for None)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多