python - "python -m doctest"忽略不同目录下的同名文件

标签 python python-import doctest

假设我们有两个文件:

失败/stuff.py

"""
>>> True
False
"""

通过/stuff.py

"""
>>> True
True
"""

然后我们在 doctest 下运行它们:

python -m doctest fail/stuff.py pass/stuff.py

正如预期的那样,我们从 fail/stuff.py 中看到一个错误。但是如果我们以相反的顺序运行它们:

python -m doctest pass/stuff.py fail/stuff.py

然后就过去了!

Python 的导入系统无法处理来自两个文件的加载测试是否存在某些根本原因,或者 doctest 是否已损坏?

最佳答案

Is there some fundamental reason why Python's import system is unable to cope with loading tests from both files, or is doctest simply broken?

Doctests 基本上就坏了。 Python 导入系统无法处理这种情况没有根本原因。因此,没有根本原因测试运行程序也不能处理这种情况。我怀疑他们在编写命令行界面时考虑得太多,因为大多数人不会直接将 doctest 用作运行器(相反,将库代码与功能更全的运行器集成并使用 doctest 插件将是更平常)。

您可以使用更好的测试运行器。例如, Nose (除其他外)不会有这个问题:

$ nosetests pass/stuff.py fail/stuff.py --with-doctest
.F
======================================================================
FAIL: Doctest: stuff
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/doctest.py", line 2199, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for stuff
  File "/private/tmp/fail/stuff.py", line 0, in stuff

----------------------------------------------------------------------
File "/private/tmp/fail/stuff.py", line 2, in stuff
Failed example:
    True
Expected:
    False
Got:
    True


----------------------------------------------------------------------
Ran 2 tests in 0.004s

FAILED (failures=1)

关于python - "python -m doctest"忽略不同目录下的同名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463273/

相关文章:

Elixir :doctesting一个印记

python - 如何使用 doctest 检查程序是否产生了某些输出?

python - 运行时检查复杂变量参数

Python 无法从同一目录导入?

python - python循环导入是一个实现细节吗?

python - 包模块应该如何相互导入?

python - 如何通过绘图通过 Python doctest

Python Popen WHOIS OS 命令失败测试

Python是强类型语言,创建DataFrame时dtype = float将应用于仅对float有意义的值?

python - "If not"python中的条件语句