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.

pytest capsys not caputuring output

See original GitHub issue

Hey,

according to the docs I was expecting the default logger to write to stderr:

you can use the Logger right after import as it comes pre-configured (logs are emitted to sys.stderr by default)

So I was running this minimal example with pytest and was expecting it to work:

from loguru import logger


def test_logger(capsys):
    logger.error("Some Error")
    out, err = capsys.readouterr()
    assert "Some Error" in err

However it fails. When I add an explicit Handler that writed to stderr, it works. Why does the default handler not work?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, Sep 18, 2022

Hi.

This is not working out of the box with capsys due to Pytest internals which replace sys.stderr with their own special object. Consequently, if logger.add() was called prior to the running the test, the Loguru’s handler is lost.

0reactions
elisiariocoutocommented, Nov 8, 2022

Sure, will do. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

stdout not captured by pytest capsys when using fixture
Here, capsys does not capture sys.stderr properly. If I move from sys import stdout and args.out = stdout directly into the test function, ......
Read more >
stdout capture (capsys) does not seem to work with real-time ...
Running with pytest --log-cli-level=INFO makes the test fail as the captured.out variable comes empty. pip list of the virtual environment you ...
Read more >
How to capture stdout/stderr output — pytest documentation
tee-sys capturing: Python writes to sys.stdout and sys.stderr will be captured, ... output is captured") with capsys.disabled(): print("output not captured, ...
Read more >
Using pytest, sometimes does not capture stderr
Using pytest, sometimes does not capture stderr ... capsys = <_pytest.capture. ... 1) The final line of the pytest failure output seems to...
Read more >
Pytest capsys - Waylon Walker
capsys is a builtin pytest fixture that can be passed into any test to capture stdin/stdout. For a more comprehensive description check out ......
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