Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No stdout/stderr is captured with invoke during tests in combination with structlog #2827

Open
ivasic opened this issue Dec 20, 2024 · 0 comments
Labels

Comments

@ivasic
Copy link

ivasic commented Dec 20, 2024

There seems to be an issue with capturing stdout/stderr when using structlog with stdlib Logger in tests.
I've raised an issue with structlog but they are not convinced the problem originates there since the logs are simply passed to the stdlib Logger.

I've traced this down to the click's isolation context manager which seems to replace sys.stdout/sys.stderr with mocks during testing and my guess would be that this could have something to do with the problem but I'm hoping you may know better.

I've tried numerous things like explicitly setting a StreamHandler(sys.stderr) to the stdlib basicConfig, experimented with mix_stderr etc. nothing helped. Any ideas how to fix or work around this issue would be most welcome, thank you.

An example

# app.py
import structlog
import click

logger = structlog.get_logger()
structlog.configure(logger_factory=structlog.stdlib.LoggerFactory())


@click.command()
def hello() -> None:
    logger.info("Hello!")

if __name__ == '__main__':
    hello()

A test case:

# test_app.py
from click.testing import CliRunner
from app import hello

def test_log() -> None:
    runner = CliRunner()
    result = runner.invoke(hello)
    print(result.stdout)
    assert "Hello" in result.stdout

Expected
Hello! (to be printed)
assertion to pass

Actual
nothing printed (result.stdout is empty)
assertion failure:

>       assert "Hello" in result.stdout
E       AssertionError: assert 'Hello' in ''
E        +  where '' = <Result okay>.stdout

test_app.py:8: AssertionError```

Environment: macOS

- Python version: 3.12.7
- Click version: 8.1.7
@Rowlando13 Rowlando13 added the bug label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants