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
| 616 | |
| 617 | |
| 618 | class CaptureManager: |
| 619 | """The capture plugin. |
| 620 | |
| 621 | Manages that the appropriate capture method is enabled/disabled during |
| 622 | collection and each test phase (setup, call, teardown). After each of |
| 623 | those points, the captured output is obtained and attached to the |
| 624 | collection/runtest report. |
| 625 | |
| 626 | There are two levels of capture: |
| 627 | |
| 628 | * global: enabled by default and can be suppressed by the ``-s`` |
| 629 | option. This is always enabled/disabled during collection and each test |
| 630 | phase. |
| 631 | |
| 632 | * fixture: when a test function or one of its fixture depend on the |
| 633 | ``capsys`` or ``capfd`` fixtures. In this case special handling is |
| 634 | needed to ensure the fixtures take precedence over the global capture. |
| 635 | """ |
| 636 | |
| 637 | def __init__(self, method: "_CaptureMethod") -> None: |
| 638 | self._method = method |
| 639 | self._global_capturing: Optional[MultiCapture[str]] = None |
| 640 | self._capture_fixture: Optional[CaptureFixture[Any]] = None |
| 641 | |
| 642 | def __repr__(self) -> str: |
| 643 | return "<CaptureManager _method={!r} _global_capturing={!r} _capture_fixture={!r}>".format( |
| 644 | self._method, self._global_capturing, self._capture_fixture |
| 645 | ) |
| 646 | |
| 647 | def is_capturing(self) -> Union[str, bool]: |
| 648 | if self.is_globally_capturing(): |
| 649 | return "global" |
| 650 | if self._capture_fixture: |
| 651 | return "fixture %s" % self._capture_fixture.request.fixturename |
| 652 | return False |
| 653 | |
| 654 | # Global capturing control |
| 655 | |
| 656 | def is_globally_capturing(self) -> bool: |
| 657 | return self._method != "no" |
| 658 | |
| 659 | def start_global_capturing(self) -> None: |
| 660 | assert self._global_capturing is None |
| 661 | self._global_capturing = _get_multicapture(self._method) |
| 662 | self._global_capturing.start_capturing() |
| 663 | |
| 664 | def stop_global_capturing(self) -> None: |
| 665 | if self._global_capturing is not None: |
| 666 | self._global_capturing.pop_outerr_to_orig() |
| 667 | self._global_capturing.stop_capturing() |
| 668 | self._global_capturing = None |
| 669 | |
| 670 | def resume_global_capture(self) -> None: |
| 671 | # During teardown of the python process, and on rare occasions, capture |
| 672 | # attributes can be `None` while trying to resume global capture. |
| 673 | if self._global_capturing is not None: |
| 674 | self._global_capturing.resume_capturing() |
| 675 |
no outgoing calls