pytest-dev / pytest-dev/pytest

Way to disable stdout capturing from outside of a test

Open
#9,775 3 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: capture type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the capsys fixture behavior and the -s/--capture=tee-sys alternatives described in the issue, then trace pytest's capture lifecycle. Done means a public way to temporarily bypass capture from outside a test, with focused coverage that preserves normal capture and avoids the noise of globally disabling it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.