9
9
from typing import Callable
10
10
from typing import ContextManager
11
11
from typing import Generator
12
+ from typing import Iterator
12
13
from typing import Mapping
13
14
from typing import TYPE_CHECKING
14
15
from unittest import TestCase
@@ -175,50 +176,6 @@ class SubTests:
175
176
def item (self ) -> pytest .Item :
176
177
return self .request .node
177
178
178
- @contextmanager
179
- def _capturing_output (self ) -> Generator [Captured , None , None ]:
180
- option = self .request .config .getoption ("capture" , None )
181
-
182
- # capsys or capfd are active, subtest should not capture
183
-
184
- capman = self .request .config .pluginmanager .getplugin ("capturemanager" )
185
- capture_fixture_active = getattr (capman , "_capture_fixture" , None )
186
-
187
- if option == "sys" and not capture_fixture_active :
188
- with ignore_pytest_private_warning ():
189
- fixture = CaptureFixture (SysCapture , self .request )
190
- elif option == "fd" and not capture_fixture_active :
191
- with ignore_pytest_private_warning ():
192
- fixture = CaptureFixture (FDCapture , self .request )
193
- else :
194
- fixture = None
195
-
196
- if fixture is not None :
197
- fixture ._start ()
198
-
199
- captured = Captured ()
200
- try :
201
- yield captured
202
- finally :
203
- if fixture is not None :
204
- out , err = fixture .readouterr ()
205
- fixture .close ()
206
- captured .out = out
207
- captured .err = err
208
-
209
- @contextmanager
210
- def _capturing_logs (self ) -> Generator [CapturedLogs | NullCapturedLogs , None , None ]:
211
- logging_plugin = self .request .config .pluginmanager .getplugin ("logging-plugin" )
212
- if logging_plugin is None :
213
- yield NullCapturedLogs ()
214
- else :
215
- handler = LogCaptureHandler ()
216
- handler .setFormatter (logging_plugin .formatter )
217
-
218
- captured_logs = CapturedLogs (handler )
219
- with catching_logs (handler ):
220
- yield captured_logs
221
-
222
179
def test (
223
180
self ,
224
181
msg : str | None = None ,
@@ -239,8 +196,6 @@ def test(
239
196
self .ihook ,
240
197
msg ,
241
198
kwargs ,
242
- capturing_output_ctx = self ._capturing_output ,
243
- capturing_logs_ctx = self ._capturing_logs ,
244
199
request = self .request ,
245
200
suspend_capture_ctx = self .suspend_capture_ctx ,
246
201
)
@@ -260,8 +215,6 @@ class _SubTestContextManager:
260
215
ihook : pluggy .HookRelay
261
216
msg : str | None
262
217
kwargs : dict [str , Any ]
263
- capturing_output_ctx : Callable [[], ContextManager ]
264
- capturing_logs_ctx : Callable [[], ContextManager ]
265
218
suspend_capture_ctx : Callable [[], ContextManager ]
266
219
request : SubRequest
267
220
@@ -274,9 +227,11 @@ def __enter__(self) -> None:
274
227
275
228
self ._exit_stack = ExitStack ()
276
229
self ._captured_output = self ._exit_stack .enter_context (
277
- self .capturing_output_ctx ()
230
+ capturing_output (self .request )
231
+ )
232
+ self ._captured_logs = self ._exit_stack .enter_context (
233
+ capturing_logs (self .request )
278
234
)
279
- self ._captured_logs = self ._exit_stack .enter_context (self .capturing_logs_ctx ())
280
235
281
236
def __exit__ (
282
237
self ,
@@ -342,6 +297,53 @@ def make_call_info(
342
297
)
343
298
344
299
300
+ @contextmanager
301
+ def capturing_output (request : SubRequest ) -> Iterator [Captured ]:
302
+ option = request .config .getoption ("capture" , None )
303
+
304
+ # capsys or capfd are active, subtest should not capture.
305
+ capman = request .config .pluginmanager .getplugin ("capturemanager" )
306
+ capture_fixture_active = getattr (capman , "_capture_fixture" , None )
307
+
308
+ if option == "sys" and not capture_fixture_active :
309
+ with ignore_pytest_private_warning ():
310
+ fixture = CaptureFixture (SysCapture , request )
311
+ elif option == "fd" and not capture_fixture_active :
312
+ with ignore_pytest_private_warning ():
313
+ fixture = CaptureFixture (FDCapture , request )
314
+ else :
315
+ fixture = None
316
+
317
+ if fixture is not None :
318
+ fixture ._start ()
319
+
320
+ captured = Captured ()
321
+ try :
322
+ yield captured
323
+ finally :
324
+ if fixture is not None :
325
+ out , err = fixture .readouterr ()
326
+ fixture .close ()
327
+ captured .out = out
328
+ captured .err = err
329
+
330
+
331
+ @contextmanager
332
+ def capturing_logs (
333
+ request : SubRequest ,
334
+ ) -> Iterator [CapturedLogs | NullCapturedLogs ]:
335
+ logging_plugin = request .config .pluginmanager .getplugin ("logging-plugin" )
336
+ if logging_plugin is None :
337
+ yield NullCapturedLogs ()
338
+ else :
339
+ handler = LogCaptureHandler ()
340
+ handler .setFormatter (logging_plugin .formatter )
341
+
342
+ captured_logs = CapturedLogs (handler )
343
+ with catching_logs (handler ):
344
+ yield captured_logs
345
+
346
+
345
347
@contextmanager
346
348
def ignore_pytest_private_warning () -> Generator [None , None , None ]:
347
349
import warnings
0 commit comments