Skip to content

Way to disable stdout capturing from outside of a test #9775

Open
@dzcode

Description

@dzcode

What's the problem this feature will solve?

If an env var is set, I'd like to open a port for remote debugging. In doing so, I'd like to prompt in the terminal that it is waiting to start, like this below, in a top-level __init__.py file of the app:

import logging
import os
logger = logging.getLogger(__name__)
def setup_debugger():
    if os.environ.get("REMOTE_DEBUG"):
        import debugpy
        port = 5678
        logger.warning(f"Waiting for remote debugger to connect on port {port}")
        debugpy.listen(("0.0.0.0", port))
        debugpy.wait_for_client()
setup_debugger()

However, pytest swallows the output, so the terminal output is just hanging blank:

$ pytest

# hanging

Describe the solution you'd like

In a test, I'd typically be able to temporarily silence the output swallowing via the capsys.disabled():

def test_disabling_capturing(capsys):
    print("this output is captured")
    with capsys.disabled():
        print("output not captured, going directly to sys.stdout")
    print("this output is also captured")

However, capsys appears only available as a test fixture, and I don't see a public API to disable stdout capture from outside of a test. I'd like a similar API to do so, e.g.:

from pytest import capsys

with capsys.disabled():
    logger.warning(f"Waiting for remote debugger to connect on port {port}")

See above in problem description

Alternative Solutions

I can run with the -s flag (or --capture=tee-sys), but that causes noisy output since I only care about printing errors from outside of test runs.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin plugintype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions