question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve error message when tests use closed stdout/stderr (capture)

See original GitHub issue

@nicoddemus @asottile

This is still an issue with the latest pytest across tests (e.g. when one tests change the logging config and add a stream handler pointing to stdout, a subsequent test that doesn’t setup logging and tries to write to logging will fail).

I think the issue boils down to the fact that if you set up a Stream Handler and you enable capsys the actual output stream will be a pytest stream that will be closed and thrown away at the next test in the test suite. So when a subsequent test tries to write to it you get this error. Adding the following fixture fixed this:

LOGGER = logging.getLogger()

@pytest.fixture(autouse=True)
def ensure_logging_framework_not_altered():
    before_handlers = list(LOGGER.handlers)
    yield
    LOGGER.handlers = before_handlers

Should this be automatically done always by pytest? Granted for this to work ideally you would need to do it for all loggers, not just the root one. In this case, for me, this was enough as I was only set up a custom stream logger onto the root logger.

In an ideal world, I would expect though pytest to cleanup loggig configurations at the end tests that requested the caplog fixture.

_Originally posted by @gaborbernat in https://github.com/pytest-dev/pytest/issues/14#issuecomment-521577819_

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:29 (25 by maintainers)

github_iconTop GitHub Comments

3reactions
asottilecommented, Aug 15, 2019

yeah I’m fine with a better error message – somehow notify the user that they’re reassigning globals to sys.std* streams as a test side-effect

0reactions
karthikvadlacommented, Mar 17, 2020

I don’t think pytest should automatically clean up any of a user’s resources as it’s arguably a correctness problem with the test suite or user code

@asottile The fixture works only when logging is set at function-level, when i define logging at module level, still tests fail with error https://github.com/pytest-dev/pytest/issues/5577

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to capture stdout/stderr with googletest? - Stack Overflow
For example, I would like to call a function that writes errors to the console (stderr). Now, when calling the function in the...
Read more >
Cannot test output on stderr and stdout using googletest.
ASSERT_DEATH() will run the expression in a child process. Capture*() captures the stdout/err of this process and it will not include anything ...
Read more >
How to capture stdout/stderr output — pytest documentation
During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails its according captured...
Read more >
Errors | Node.js v19.3.0 Documentation
Error objects capture a "stack trace" detailing the point in the code at which the Error was instantiated, and may provide a text...
Read more >
bin/sh: capture stderr into a variable - Unix Stack Exchange
Honestly, using a file might be the simplest way to go. But let's make some assumptions here, and say you want stdout as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found