MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_live_logging_suspends_capture

Function test_live_logging_suspends_capture

testing/logging/test_reporting.py:822–867  ·  view source on GitHub ↗

Test that capture manager is suspended when we emitting messages for live logging. This tests the implementation calls instead of behavior because it is difficult/impossible to do it using ``pytester`` facilities because they do their own capturing. We parametrize the test to also make

(
    has_capture_manager: bool, request: FixtureRequest
)

Source from the content-addressed store, hash-verified

820
821@pytest.mark.parametrize("has_capture_manager", [True, False])
822def test_live_logging_suspends_capture(
823 has_capture_manager: bool, request: FixtureRequest
824) -> None:
825 """Test that capture manager is suspended when we emitting messages for live logging.
826
827 This tests the implementation calls instead of behavior because it is difficult/impossible to do it using
828 ``pytester`` facilities because they do their own capturing.
829
830 We parametrize the test to also make sure _LiveLoggingStreamHandler works correctly if no capture manager plugin
831 is installed.
832 """
833 import logging
834 import contextlib
835 from functools import partial
836 from _pytest.logging import _LiveLoggingStreamHandler
837
838 class MockCaptureManager:
839 calls = []
840
841 @contextlib.contextmanager
842 def global_and_fixture_disabled(self):
843 self.calls.append("enter disabled")
844 yield
845 self.calls.append("exit disabled")
846
847 class DummyTerminal(io.StringIO):
848 def section(self, *args, **kwargs):
849 pass
850
851 out_file = cast(TerminalReporter, DummyTerminal())
852 capture_manager = (
853 cast(CaptureManager, MockCaptureManager()) if has_capture_manager else None
854 )
855 handler = _LiveLoggingStreamHandler(out_file, capture_manager)
856 handler.set_when("call")
857
858 logger = logging.getLogger(__name__ + ".test_live_logging_suspends_capture")
859 logger.addHandler(handler)
860 request.addfinalizer(partial(logger.removeHandler, handler))
861
862 logger.critical("some message")
863 if has_capture_manager:
864 assert MockCaptureManager.calls == ["enter disabled", "exit disabled"]
865 else:
866 assert MockCaptureManager.calls == []
867 assert cast(io.StringIO, out_file).getvalue() == "\nsome message\n"
868
869
870def test_collection_live_logging(pytester: Pytester) -> None:

Callers

nothing calls this directly

Calls 6

DummyTerminalClass · 0.85
MockCaptureManagerClass · 0.85
set_whenMethod · 0.45
addfinalizerMethod · 0.45
getvalueMethod · 0.45

Tested by

no test coverage detected