Skip to content

Commit acab13f

Browse files
authored
Add new filtering() method to LogCaptureFixture class (#11625)
Fixes #11610
1 parent a42530a commit acab13f

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ Javier Romero
188188
Jeff Rackauckas
189189
Jeff Widman
190190
Jenni Rinker
191+
Jens Tröger
191192
John Eddie Ayson
192193
John Litborn
193194
John Towler

changelog/11610.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added :func:`LogCaptureFixture.filtering() <pytest.LogCaptureFixture.filtering>` context manager that
2+
adds a given :class:`logging.Filter` object to the caplog fixture.

src/_pytest/logging.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,22 @@ def at_level(
564564
self.handler.setLevel(handler_orig_level)
565565
logging.disable(original_disable_level)
566566

567+
@contextmanager
568+
def filtering(self, filter_: logging.Filter) -> Generator[None, None, None]:
569+
"""Context manager that temporarily adds the given filter to the caplog's
570+
:meth:`handler` for the 'with' statement block, and removes that filter at the
571+
end of the block.
572+
573+
:param filter_: A custom :class:`logging.Filter` object.
574+
575+
.. versionadded:: 7.5
576+
"""
577+
self.handler.addFilter(filter_)
578+
try:
579+
yield
580+
finally:
581+
self.handler.removeFilter(filter_)
582+
567583

568584
@fixture
569585
def caplog(request: FixtureRequest) -> Generator[LogCaptureFixture, None, None]:

testing/logging/test_fixture.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test3(caplog):
144144
result.assert_outcomes(passed=3)
145145

146146

147-
def test_with_statement(caplog: pytest.LogCaptureFixture) -> None:
147+
def test_with_statement_at_level(caplog: pytest.LogCaptureFixture) -> None:
148148
with caplog.at_level(logging.INFO):
149149
logger.debug("handler DEBUG level")
150150
logger.info("handler INFO level")
@@ -159,7 +159,9 @@ def test_with_statement(caplog: pytest.LogCaptureFixture) -> None:
159159
assert "CRITICAL" in caplog.text
160160

161161

162-
def test_with_statement_logging_disabled(caplog: pytest.LogCaptureFixture) -> None:
162+
def test_with_statement_at_level_logging_disabled(
163+
caplog: pytest.LogCaptureFixture,
164+
) -> None:
163165
logging.disable(logging.CRITICAL)
164166
assert logging.root.manager.disable == logging.CRITICAL
165167
with caplog.at_level(logging.WARNING):
@@ -185,6 +187,22 @@ def test_with_statement_logging_disabled(caplog: pytest.LogCaptureFixture) -> No
185187
assert logging.root.manager.disable == logging.CRITICAL
186188

187189

190+
def test_with_statement_filtering(caplog: pytest.LogCaptureFixture) -> None:
191+
class TestFilter(logging.Filter):
192+
def filter(self, record: logging.LogRecord) -> bool:
193+
record.msg = "filtered handler call"
194+
return True
195+
196+
with caplog.at_level(logging.INFO):
197+
with caplog.filtering(TestFilter()):
198+
logger.info("handler call")
199+
logger.info("handler call")
200+
201+
filtered_tuple, unfiltered_tuple = caplog.record_tuples
202+
assert filtered_tuple == ("test_fixture", 20, "filtered handler call")
203+
assert unfiltered_tuple == ("test_fixture", 20, "handler call")
204+
205+
188206
@pytest.mark.parametrize(
189207
"level_str,expected_disable_level",
190208
[

0 commit comments

Comments
 (0)