The capture plugin. Manages that the appropriate capture method is enabled/disabled during collection and each test phase (setup, call, teardown). After each of those points, the captured output is obtained and attached to the collection/runtest report. There are two levels of
| 727 | |
| 728 | |
| 729 | class CaptureManager: |
| 730 | """The capture plugin. |
| 731 | |
| 732 | Manages that the appropriate capture method is enabled/disabled during |
| 733 | collection and each test phase (setup, call, teardown). After each of |
| 734 | those points, the captured output is obtained and attached to the |
| 735 | collection/runtest report. |
| 736 | |
| 737 | There are two levels of capture: |
| 738 | |
| 739 | * global: enabled by default and can be suppressed by the ``-s`` |
| 740 | option. This is always enabled/disabled during collection and each test |
| 741 | phase. |
| 742 | |
| 743 | * fixture: when a test function or one of its fixture depend on the |
| 744 | ``capsys`` or ``capfd`` fixtures. In this case special handling is |
| 745 | needed to ensure the fixtures take precedence over the global capture. |
| 746 | """ |
| 747 | |
| 748 | def __init__(self, method: _CaptureMethod) -> None: |
| 749 | self._method: Final = method |
| 750 | self._global_capturing: MultiCapture[str] | None = None |
| 751 | self._capture_fixture: CaptureFixture[Any] | None = None |
| 752 | |
| 753 | def __repr__(self) -> str: |
| 754 | return ( |
| 755 | f"<CaptureManager _method={self._method!r} _global_capturing={self._global_capturing!r} " |
| 756 | f"_capture_fixture={self._capture_fixture!r}>" |
| 757 | ) |
| 758 | |
| 759 | def is_capturing(self) -> str | bool: |
| 760 | if self.is_globally_capturing(): |
| 761 | return "global" |
| 762 | if self._capture_fixture: |
| 763 | return f"fixture {self._capture_fixture.request.fixturename}" |
| 764 | return False |
| 765 | |
| 766 | # Global capturing control |
| 767 | |
| 768 | def is_globally_capturing(self) -> bool: |
| 769 | return self._method != "no" |
| 770 | |
| 771 | def start_global_capturing(self) -> None: |
| 772 | assert self._global_capturing is None |
| 773 | self._global_capturing = _get_multicapture(self._method) |
| 774 | self._global_capturing.start_capturing() |
| 775 | |
| 776 | def stop_global_capturing(self) -> None: |
| 777 | if self._global_capturing is not None: |
| 778 | self._global_capturing.pop_outerr_to_orig() |
| 779 | self._global_capturing.stop_capturing() |
| 780 | self._global_capturing = None |
| 781 | |
| 782 | def resume_global_capture(self) -> None: |
| 783 | # During teardown of the python process, and on rare occasions, capture |
| 784 | # attributes can be `None` while trying to resume global capture. |
| 785 | if self._global_capturing is not None: |
| 786 | self._global_capturing.resume_capturing() |
no outgoing calls