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
)
| 924 | |
| 925 | @pytest.mark.parametrize("has_capture_manager", [True, False]) |
| 926 | def test_live_logging_suspends_capture( |
| 927 | has_capture_manager: bool, request: FixtureRequest |
| 928 | ) -> None: |
| 929 | """Test that capture manager is suspended when we emitting messages for live logging. |
| 930 | |
| 931 | This tests the implementation calls instead of behavior because it is difficult/impossible to do it using |
| 932 | ``pytester`` facilities because they do their own capturing. |
| 933 | |
| 934 | We parametrize the test to also make sure _LiveLoggingStreamHandler works correctly if no capture manager plugin |
| 935 | is installed. |
| 936 | """ |
| 937 | import contextlib |
| 938 | from functools import partial |
| 939 | import logging |
| 940 | |
| 941 | from _pytest.logging import _LiveLoggingStreamHandler |
| 942 | |
| 943 | class MockCaptureManager: |
| 944 | calls = [] |
| 945 | |
| 946 | @contextlib.contextmanager |
| 947 | def global_and_fixture_disabled(self): |
| 948 | self.calls.append("enter disabled") |
| 949 | yield |
| 950 | self.calls.append("exit disabled") |
| 951 | |
| 952 | class DummyTerminal(io.StringIO): |
| 953 | def section(self, *args, **kwargs): |
| 954 | pass |
| 955 | |
| 956 | out_file = cast(TerminalReporter, DummyTerminal()) |
| 957 | capture_manager = ( |
| 958 | cast(CaptureManager, MockCaptureManager()) if has_capture_manager else None |
| 959 | ) |
| 960 | handler = _LiveLoggingStreamHandler(out_file, capture_manager) |
| 961 | handler.set_when("call") |
| 962 | |
| 963 | logger = logging.getLogger(__name__ + ".test_live_logging_suspends_capture") |
| 964 | logger.addHandler(handler) |
| 965 | request.addfinalizer(partial(logger.removeHandler, handler)) |
| 966 | |
| 967 | logger.critical("some message") |
| 968 | if has_capture_manager: |
| 969 | assert MockCaptureManager.calls == ["enter disabled", "exit disabled"] |
| 970 | else: |
| 971 | assert MockCaptureManager.calls == [] |
| 972 | assert cast(io.StringIO, out_file).getvalue() == "\nsome message\n" |
| 973 | |
| 974 | |
| 975 | def test_collection_live_logging(pytester: Pytester) -> None: |
nothing calls this directly
no test coverage detected