简体   繁体   中英

doctest exception output not reported under pytest

I'm experiencing a lost traceback from a doctest when running them under Pytest. 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:

>>> 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:

$ 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? 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.

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? ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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