17
17
import pytest
18
18
from _pytest .compat import CaptureAndPassthroughIO
19
19
from _pytest .compat import CaptureIO
20
+ from _pytest .compat import TYPE_CHECKING
20
21
from _pytest .config import Config
21
22
from _pytest .fixtures import FixtureRequest
22
23
24
+ if TYPE_CHECKING :
25
+ from typing_extensions import Literal
26
+
27
+ _CaptureMethod = Literal ["fd" , "sys" , "no" , "tee-sys" ]
28
+
23
29
patchsysdict = {0 : "stdin" , 1 : "stdout" , 2 : "stderr" }
24
30
25
31
@@ -66,6 +72,18 @@ def pytest_load_initial_conftests(early_config: Config):
66
72
sys .stderr .write (err )
67
73
68
74
75
+ def _get_multicapture (method : "_CaptureMethod" ) -> "MultiCapture" :
76
+ if method == "fd" :
77
+ return MultiCapture (out = True , err = True , Capture = FDCapture )
78
+ elif method == "sys" :
79
+ return MultiCapture (out = True , err = True , Capture = SysCapture )
80
+ elif method == "no" :
81
+ return MultiCapture (out = False , err = False , in_ = False )
82
+ elif method == "tee-sys" :
83
+ return MultiCapture (out = True , err = True , in_ = False , Capture = TeeSysCapture )
84
+ raise ValueError ("unknown capturing method: {!r}" .format (method ))
85
+
86
+
69
87
class CaptureManager :
70
88
"""
71
89
Capture plugin, manages that the appropriate capture method is enabled/disabled during collection and each
@@ -79,7 +97,7 @@ class CaptureManager:
79
97
case special handling is needed to ensure the fixtures take precedence over the global capture.
80
98
"""
81
99
82
- def __init__ (self , method ) -> None :
100
+ def __init__ (self , method : "_CaptureMethod" ) -> None :
83
101
self ._method = method
84
102
self ._global_capturing = None
85
103
self ._capture_fixture = None # type: Optional[CaptureFixture]
@@ -89,17 +107,6 @@ def __repr__(self):
89
107
self ._method , self ._global_capturing , self ._capture_fixture
90
108
)
91
109
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
-
103
110
def is_capturing (self ):
104
111
if self .is_globally_capturing ():
105
112
return "global"
@@ -114,7 +121,7 @@ def is_globally_capturing(self):
114
121
115
122
def start_global_capturing (self ):
116
123
assert self ._global_capturing is None
117
- self ._global_capturing = self . _getcapture (self ._method )
124
+ self ._global_capturing = _get_multicapture (self ._method )
118
125
self ._global_capturing .start_capturing ()
119
126
120
127
def stop_global_capturing (self ):
0 commit comments