-
Hi pytest-experts :) i like to use the caplog fixture in another fixture with session scope: import pytest
import logging
@pytest.fixture(scope='session')
def my_log(caplog):
caplog.set_level(logging.INFO)
def test_foo(my_log):
pass This results in the following error: |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
thats currently not supported |
Beta Was this translation helpful? Give feedback.
-
You can't use function-scoped fixtures in session-scoped fixtures, because there's no correct time to create or destroy the result! Fortunately there's an easy fix here - just define |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
@Forty-Bot, where does that code get put? |
Beta Was this translation helpful? Give feedback.
-
Anywhere you want, really. I made it a context manager so I can use it with |
Beta Was this translation helpful? Give feedback.
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:
This can also be trivially extended as a context manager. You may want to call
_finalize
if you modify the log level.