6

We're using pytest and python std logging, and have some tests in doctests. We'd like to enable log_cli to make debugging tests in ide easier (lets stderr flow to the "live" console so one can see log statements as they are output when stepping through) The problem is there appears to be a bug/iteraction between "use of logging" (eg presence of a call to logger.info("...") etc) log_cli=true.

I don't see any other flags or mention of this in the docs, so it appears to be a bug, but was hoping there is a workaround.

This test module passes:

# bugjar.py
"""
>>> dummy()
'retval'
"""
import logging
logger = logging.getLogger(__name__)
def dummy():
    # logger.info("AnInfoLog")  ## un-comment to break test
    return "retval"

but un-commenting (no other changes) the logger.info( call causes a failure: (unless i remove log_cli from pytest.ini)

002 >>> dummy()
Expected:
    'retval'
Got nothing

Here is my command line & relevant version output(s):

(venv) $ ./venv/bin/pytest -c ./pytest.ini ./bugjar.py
======================================================================= test session starts ========================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: -omitted-/test-unit, configfile: ./pytest.ini
plugins: forked-1.3.0, xdist-2.2.1, profiling-1.7.0
collected 1 item                                                                                                                                                   

bugjar.py::bugjar
 
bugjar.py::bugjar 
-------------------------------------------------------------------------- live log call ---------------------------------------------------------------------------
INFO     bugjar:bugjar.py:8 AnInfoLog
FAILED                                                                                                                                                       [100%]

============================================================================= FAILURES =============================================================================
_________________________________________________________________________ [doctest] bugjar _________________________________________________________________________
001 
002 >>> dummy()
Expected:
    'retval'
Got nothing

and my pytest.ini (Note, commments are correct, pass/fail is not affected by use of logfile or other addopts

[pytest]
addopts = --doctest-modules # --profile --profile-svg 
norecursedirs = logs bin tmp* scratch venv* data
# log_file = pytest.log
log_cli = true
log_cli_level=debug

removing log_cli* from pytest.ini makes the issue go away.

This seems clearly related to what log_cli is manipulating, in capturing output for use in the doctest itself, but also not the expected behavior.

I am hoping I've made a mistake, or there is a workaround to get live log output in bash or IDE shell window / debugger.

1
  • 1
    pytest#5908 tracks this bug. I haven't yet figured out any work-around for this.
    – akaihola
    Commented Jan 1, 2023 at 16:30

1 Answer 1

0

Sorry theY4Kman for reposting your solution from GitHub (please post it yourself and I'll remove mine):

One way to work around this is to disable capturing for doctest items, using the pytest_runtest_call hook:

import sys

from _pytest.config import hookimpl
from _pytest.doctest import DoctestItem
from _pytest.nodes import Item


@hookimpl(hookwrapper=True)
def pytest_runtest_call(item: Item) -> None:
    """Hook to suspend global capture when running doctests"""
    if isinstance(item, DoctestItem):
        capman = item.config.pluginmanager.getplugin('capturemanager')
        if capman:
            capman.suspend_global_capture(in_=True)
            out, err = capman.read_global_capture()
            sys.stdout.write(out)
            sys.stderr.write(err)

    yield

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.