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.

Output appears in "Captured stderr call" but is unavailable in capsys or capfd

See original GitHub issue

Environment

Pytest version: 5.2.1 Operating system: Kubuntu 18.04.3 LTS Pip list:

Package            Version
------------------ -------
atomicwrites       1.3.0
attrs              19.3.0
importlib-metadata 0.23
more-itertools     7.2.0
packaging          19.2
pip                19.3.1
pluggy             0.13.0
py                 1.8.0
pyparsing          2.4.2
pytest             5.2.1
setuptools         41.4.0
six                1.12.0
wcwidth            0.1.7
wheel              0.33.6
zipp               0.6.0

Description

It seems that output can appear in “Captured stderr call” but be unavailable to capsys or capfd, which can be a bug? It’s definitely weird.

I came accross this issue while trying to test my logging setup. This is a minimal example I came up with (available at https://github.com/butla/experiments/tree/master/pytest_capture_log_error):

# a_file.py
import logging

logging.basicConfig(level=logging.INFO)

LOG_MESSAGE = "the message we'll look for in the test"

_log = logging.getLogger(__name__)


def bla():
    _log.info(LOG_MESSAGE)
    return 5
# test_file.py
import a_file

def test_a(capsys):
    assert a_file.bla() == 5
    assert a_file.LOG_MESSAGE in capsys.readouterr().err

The test fails and it shows

------------------------------------------ Captured stderr call ------------------------------------------
INFO:a_file:the message we'll look for in the test

but both capsys and capfd show out and err to be empty.

I would use caplog, but the messages I get from it don’t conform to the log configuration that I have set up in my original app, so I decided to just use stderr.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:18 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
asottilecommented, Apr 26, 2021

@RonnyPfannschmidt my reproduction above does not use logging – this is definitely something capfd should be able to handle. pytest almost-mockingly displays “Captured stdout” “wat” when it fails so pytest definitely could capture it properly!

2reactions
Kachecommented, Feb 7, 2022

I think I ran into the same issue, my repro:

# foo.py
print("hello")

Surprisingly:

from importlib import import_module

def test_prints_hello(capfd):
    import_module('foo')
    out, err = capfd.readouterr()

    assert out == ""  # will pass
    # assert out == "hello\n"  # and so will this one (expected), but only if above line is commented out

edit Although this is still happening in the particular project I used, I’m not able to reproduce this in an fresh & independent venv. Very strange…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to capture stdout/stderr output — pytest documentation
The readouterr() call snapshots the output so far - and capturing will be continued. After the test function finishes the original streams will...
Read more >
stdout and stderr capture with capsys - python - Stack Overflow
I am using pytest to validate this code and capturing the output using capsys fixture. But capsys.readouterr() is not capturing the output. Code ......
Read more >
Pytest Capture Not Working - Caplog And Capsys Are Empty
It seems that output can appear in "Captured stderr call" but be unavailable to capsys or capfd, which can be a bug? It's...
Read more >
pytest Documentation - Read the Docs
The capsys, capsysbinary, capfd, and capfdbinary fixtures allow access to stdout/stderr output created during test execution.
Read more >
Using pytest, sometimes does not capture stderr
Is it because stderr during module import is not the same as during test? Is it something to do with mutable defaults? How...
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