logging: reuse LoggingCaptureHandler instance since it's expensive to create

Previously, a LoggingCaptureHandler was instantiated for each test's
setup/call/teardown which turns out to be expensive.

Instead, only keep one instance and reset it between runs.
This commit is contained in:
Ran Benita
2020-05-13 01:29:25 +03:00
parent 9310d67773
commit d2d11a8bdc
2 changed files with 11 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
import logging
import pytest
from _pytest.logging import catch_log_handlers_key
from _pytest.logging import catch_log_records_key
logger = logging.getLogger(__name__)
sublogger = logging.getLogger(__name__ + ".baz")
@@ -137,4 +137,4 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
# This reaches into private API, don't use this type of thing in real tests!
assert set(caplog._item._store[catch_log_handlers_key]) == {"setup", "call"}
assert set(caplog._item._store[catch_log_records_key]) == {"setup", "call"}