You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromloguruimportloggerdeftest_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()
assertmessageinout
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)
assertmessageincaplog.text
And it passes :) . The fun is that he don't test sys.stdout with this approach.
The text was updated successfully, but these errors were encountered:
ixenion
changed the title
pytest logger
pytest logger stdout sink
Aug 31, 2024
The problem with your test is that, at the time the test function is called, the logger is already configured with the sys.stdout default sink. Therefore, although capsys will replace sys.stdout for testing purpose, the logger won't perceive this change. The logger will write to an outdated reference of sys.stdout, and capsys won't capture the logs.
As a workaround, you actually need to call logger.add(sys.stdout) within your test (despite your comment in code stating the opposite, sorry :) ):
Hey, what's up!
How did you manage to conduct sink=sys.stdout test like in your tests/test_add_sinks.py:
?
Because when I create my own test function:
I got accert error:
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:
And it passes :) . The fun is that he don't test sys.stdout with this approach.
The text was updated successfully, but these errors were encountered: