Skip to content

caplog: Use 'session' scope #11177

Answered by Forty-Bot
MarBra asked this question in General
Discussion options

You must be logged in to vote

This is still an issue. Sometimes you need to capture log output and you can't make the fixture session-scoped (such as when it involves a lot of work to set up). For these cases, I present the following workaround:

request.node.add_report_section = lambda *args: None
logging_plugin = request.config.pluginmanager.getplugin('logging-plugin')
for _ in logging_plugin.pytest_runtest_setup(request.node):
   caplog = pytest.LogCaptureFixture(request.node, _ispytest=True)
   # do whatever you want with caplog

This can also be trivially extended as a context manager. You may want to call _finalize if you modify the log level.

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by nicoddemus
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity plugin: capture related to the capture builtin plugin topic: fixtures anything involving fixtures directly or indirectly plugin: logging related to the logging builtin plugin
5 participants
Converted from issue

This discussion was converted from issue #4502 on July 06, 2023 22:58.