简体   繁体   English

pytest下没有报告doctest异常输出

[英]doctest exception output not reported under pytest

I'm experiencing a lost traceback from a doctest when running them under Pytest. 在Pytest下运行doctest时,我遇到了回溯丢失的情况。 It's reversible state library that changes state in a reversible manner, eg this example code: 可逆状态库 ,它以可逆方式更改状态,例如,以下示例代码:

import sys
from altered import state, forget
with state(sys.modules, shutil=forget):
    import shutil
#  Traceback output....

should simulate an import problem and crash. 应该模拟一个导入问题并崩溃。 If I run it as a script in my project root directory I do get a traceback: 如果我在项目根目录中将其作为脚本运行,则会得到回溯:

Traceback (most recent call last):
  ...traceback details...
KeyError: 'shutil'

On the other hand, I have a documentation file with doctest-formatted examples in it with the following example: 另一方面,我有一个文档文件,其中包含doctest格式的示例,其中包含以下示例:

>>> import sys
>>> from altered import state, forget
>>> with state(sys.modules, shutil=forget):
...     import shutil
Traceback (most recent call last):
    ...
KeyError: 'shutil'

If I add the example file as a doctested glob to pytest, pytest complains when running it: 如果我将示例文件作为经过doctested的glob添加到pytest,则pytest在运行时会抱怨:

$ py.test docs/examples.rst
============================ test session starts =============================
platform darwin -- Python 2.7.10, pytest-3.0.5, py-1.4.30, pluggy-0.4.0
rootdir: /Users/jacob/src/oss/altered.states, inifile: pytest.ini
collected 1 items

docs/examples.rst F

================================== FAILURES ==================================
[ ... parts of documentation /w doctest ... ]
053
054     >>> import sys
055     >>> from altered import state, forget
056     >>> with state(sys.modules, shutil=forget):
Expected:
    Traceback (most recent call last):
        ...
    KeyError: 'shutil'
Got nothing

Could it be pytest's stdout capturing that's causing trouble? 可能是pytest的stdout捕获引起了麻烦吗? Or do anyone see any other obvious entry point for troubleshooting? 还是有人看到其他明显的故障排除入口?

I have tried to put breakpoints in the doctest examples of my doc file but stepping takes me in and out of pdb itself and I haven't been able to make any sense of the problem using that approach. 我试图在我的doc文件的doctest示例中放置断点,但是单步执行会使我进入和退出pdb本身,并且使用这种方法我无法理解任何问题。

Ops... 行动...

My bad: it seems I knew too little about the workings of module imports! 我的坏事:似乎我对模块导入的工作了解得很少! Apparently, removing a module from sys.modules isn't the way to block access to (unimported) modules (see eg Preventing Python code from importing certain modules? ). 显然,从sys.modules删除模块不是阻止对(未导入 )模块的访问的方法(请参阅例如, 防止Python代码导入某些模块? )。

Sorry for bothering you, although thanks for the opportunity to explore the correct way of preventing certain imports! 很抱歉打扰您,尽管感谢您有机会探索防止某些进口的正确方法!

Changing the topmost code to 将最上面的代码更改为

import sys
from altered import state
with state(sys.modules, shutil=None):
    import shutil

makes everything behave as expected. 使一切都按预期方式运行。

See also this commit for the extra curious. 另请参阅提交,以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM