Skip to content

Commit 8e54a40

Browse files
committed
capture: factor out _get_multicapture
Ref: pytest-dev#6671 (comment)
1 parent c8b4a1a commit 8e54a40

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/_pytest/capture.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ def pytest_load_initial_conftests(early_config: Config):
6666
sys.stderr.write(err)
6767

6868

69+
def _get_multicapture(method: str) -> "MultiCapture":
70+
if method == "fd":
71+
return MultiCapture(out=True, err=True, Capture=FDCapture)
72+
elif method == "sys":
73+
return MultiCapture(out=True, err=True, Capture=SysCapture)
74+
elif method == "no":
75+
return MultiCapture(out=False, err=False, in_=False)
76+
elif method == "tee-sys":
77+
return MultiCapture(out=True, err=True, in_=False, Capture=TeeSysCapture)
78+
raise ValueError("unknown capturing method: {!r}".format(method))
79+
80+
6981
class CaptureManager:
7082
"""
7183
Capture plugin, manages that the appropriate capture method is enabled/disabled during collection and each
@@ -89,17 +101,6 @@ def __repr__(self):
89101
self._method, self._global_capturing, self._capture_fixture
90102
)
91103

92-
def _getcapture(self, method):
93-
if method == "fd":
94-
return MultiCapture(out=True, err=True, Capture=FDCapture)
95-
elif method == "sys":
96-
return MultiCapture(out=True, err=True, Capture=SysCapture)
97-
elif method == "no":
98-
return MultiCapture(out=False, err=False, in_=False)
99-
elif method == "tee-sys":
100-
return MultiCapture(out=True, err=True, in_=False, Capture=TeeSysCapture)
101-
raise ValueError("unknown capturing method: %r" % method) # pragma: no cover
102-
103104
def is_capturing(self):
104105
if self.is_globally_capturing():
105106
return "global"
@@ -114,7 +115,7 @@ def is_globally_capturing(self):
114115

115116
def start_global_capturing(self):
116117
assert self._global_capturing is None
117-
self._global_capturing = self._getcapture(self._method)
118+
self._global_capturing = _get_multicapture(self._method)
118119
self._global_capturing.start_capturing()
119120

120121
def stop_global_capturing(self):

testing/test_capture.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
import pytest
1616
from _pytest import capture
17+
from _pytest.capture import _get_multicapture
1718
from _pytest.capture import CaptureManager
19+
from _pytest.capture import MultiCapture
1820
from _pytest.config import ExitCode
1921

2022
# note: py.io capture tests where copied from
@@ -1563,3 +1565,10 @@ def test_encodedfile_writelines(tmpfile: BinaryIO) -> None:
15631565
tmpfile.close()
15641566
with pytest.raises(ValueError):
15651567
ef.read()
1568+
1569+
1570+
def test__get_multicapture():
1571+
assert isinstance(_get_multicapture("fd"), MultiCapture)
1572+
pytest.raises(ValueError, _get_multicapture, "unknown").match(
1573+
r"^unknown capturing method: 'unknown'"
1574+
)

0 commit comments

Comments
 (0)