loguru icon indicating copy to clipboard operation
loguru copied to clipboard

pytest logger stdout sink

Open ixenion opened this issue 5 months ago • 1 comments

Hey, what's up!

How did you manage to conduct sink=sys.stdout test like in your tests/test_add_sinks.py:

@repetitions
def test_stderr_sink(rep, capsys):
    log(sys.stderr, rep)
    out, err = capsys.readouterr()
    assert out == ""
    assert err == expected * rep

?

Because when I create my own test function:

from loguru import logger
def test_stdout_sink(capsys) -> None:
    # By default logger already writes to stdout
    # So Im not setting anything here.
    message = f"Hello"
    logger.info(message)
    out, err = capsys.readouterr()
    assert message in out

I got accert error:

accert "Hello" == ""

For some reason I cant catch logger stdout with capsys. "out" is just empty string.

What should I set to make it work?

Also there is pytest-loguru project, and what he does is add caplog.handler like so:

magic_handler_id = \
        logger.add(sink=caplog.handler, level=0)
message = f"Test info message"
logger.info(message)
assert message in caplog.text

And it passes :) . The fun is that he don't test sys.stdout with this approach.

ixenion avatar Aug 31 '24 09:08 ixenion