Skip to content

Commit f03ed7a

Browse files
committed
Refactor capturing_output and capturing_logs into functions
1 parent 5ff5be6 commit f03ed7a

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

src/pytest_subtests/plugin.py

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Callable
1010
from typing import ContextManager
1111
from typing import Generator
12+
from typing import Iterator
1213
from typing import Mapping
1314
from typing import TYPE_CHECKING
1415
from unittest import TestCase
@@ -175,50 +176,6 @@ class SubTests:
175176
def item(self) -> pytest.Item:
176177
return self.request.node
177178

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-
222179
def test(
223180
self,
224181
msg: str | None = None,
@@ -239,8 +196,6 @@ def test(
239196
self.ihook,
240197
msg,
241198
kwargs,
242-
capturing_output_ctx=self._capturing_output,
243-
capturing_logs_ctx=self._capturing_logs,
244199
request=self.request,
245200
suspend_capture_ctx=self.suspend_capture_ctx,
246201
)
@@ -260,8 +215,6 @@ class _SubTestContextManager:
260215
ihook: pluggy.HookRelay
261216
msg: str | None
262217
kwargs: dict[str, Any]
263-
capturing_output_ctx: Callable[[], ContextManager]
264-
capturing_logs_ctx: Callable[[], ContextManager]
265218
suspend_capture_ctx: Callable[[], ContextManager]
266219
request: SubRequest
267220

@@ -274,9 +227,11 @@ def __enter__(self) -> None:
274227

275228
self._exit_stack = ExitStack()
276229
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)
278234
)
279-
self._captured_logs = self._exit_stack.enter_context(self.capturing_logs_ctx())
280235

281236
def __exit__(
282237
self,
@@ -342,6 +297,53 @@ def make_call_info(
342297
)
343298

344299

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+
345347
@contextmanager
346348
def ignore_pytest_private_warning() -> Generator[None, None, None]:
347349
import warnings

0 commit comments

Comments
 (0)