Added activation/deactivation of capture fixture in logging emit.
This commit is contained in:
parent
da9d814da4
commit
2b71cb9c38
|
@ -85,6 +85,7 @@ class CaptureManager(object):
|
||||||
def __init__(self, method):
|
def __init__(self, method):
|
||||||
self._method = method
|
self._method = method
|
||||||
self._global_capturing = None
|
self._global_capturing = None
|
||||||
|
self._current_item = None
|
||||||
|
|
||||||
def _getcapture(self, method):
|
def _getcapture(self, method):
|
||||||
if method == "fd":
|
if method == "fd":
|
||||||
|
@ -121,15 +122,23 @@ class CaptureManager(object):
|
||||||
cap.suspend_capturing(in_=in_)
|
cap.suspend_capturing(in_=in_)
|
||||||
return outerr
|
return outerr
|
||||||
|
|
||||||
def activate_fixture(self, item):
|
def activate_fixture(self, item=None):
|
||||||
"""If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over
|
"""If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over
|
||||||
the global capture.
|
the global capture.
|
||||||
"""
|
"""
|
||||||
|
if item is None:
|
||||||
|
if self._current_item is None:
|
||||||
|
return
|
||||||
|
item = self._current_item
|
||||||
fixture = getattr(item, "_capture_fixture", None)
|
fixture = getattr(item, "_capture_fixture", None)
|
||||||
if fixture is not None:
|
if fixture is not None:
|
||||||
fixture._start()
|
fixture._start()
|
||||||
|
|
||||||
def deactivate_fixture(self, item):
|
def deactivate_fixture(self, item=None):
|
||||||
|
if item is None:
|
||||||
|
if self._current_item is None:
|
||||||
|
return
|
||||||
|
item = self._current_item
|
||||||
"""Deactivates the ``capsys`` or ``capfd`` fixture of this item, if any."""
|
"""Deactivates the ``capsys`` or ``capfd`` fixture of this item, if any."""
|
||||||
fixture = getattr(item, "_capture_fixture", None)
|
fixture = getattr(item, "_capture_fixture", None)
|
||||||
if fixture is not None:
|
if fixture is not None:
|
||||||
|
@ -151,6 +160,7 @@ class CaptureManager(object):
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
def pytest_runtest_setup(self, item):
|
def pytest_runtest_setup(self, item):
|
||||||
|
self._current_item = item
|
||||||
self.resume_global_capture()
|
self.resume_global_capture()
|
||||||
# no need to activate a capture fixture because they activate themselves during creation; this
|
# no need to activate a capture fixture because they activate themselves during creation; this
|
||||||
# only makes sense when a fixture uses a capture fixture, otherwise the capture fixture will
|
# only makes sense when a fixture uses a capture fixture, otherwise the capture fixture will
|
||||||
|
@ -160,6 +170,7 @@ class CaptureManager(object):
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
def pytest_runtest_call(self, item):
|
def pytest_runtest_call(self, item):
|
||||||
|
self._current_item = item
|
||||||
self.resume_global_capture()
|
self.resume_global_capture()
|
||||||
# it is important to activate this fixture during the call phase so it overwrites the "global"
|
# it is important to activate this fixture during the call phase so it overwrites the "global"
|
||||||
# capture
|
# capture
|
||||||
|
@ -169,6 +180,7 @@ class CaptureManager(object):
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
def pytest_runtest_teardown(self, item):
|
def pytest_runtest_teardown(self, item):
|
||||||
|
self._current_item = item
|
||||||
self.resume_global_capture()
|
self.resume_global_capture()
|
||||||
self.activate_fixture(item)
|
self.activate_fixture(item)
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -573,6 +573,7 @@ class _LiveLoggingStreamHandler(logging.StreamHandler):
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
if self.capture_manager is not None:
|
if self.capture_manager is not None:
|
||||||
|
self.capture_manager.deactivate_fixture()
|
||||||
self.capture_manager.suspend_global_capture()
|
self.capture_manager.suspend_global_capture()
|
||||||
try:
|
try:
|
||||||
if not self._first_record_emitted:
|
if not self._first_record_emitted:
|
||||||
|
@ -589,3 +590,4 @@ class _LiveLoggingStreamHandler(logging.StreamHandler):
|
||||||
finally:
|
finally:
|
||||||
if self.capture_manager is not None:
|
if self.capture_manager is not None:
|
||||||
self.capture_manager.resume_global_capture()
|
self.capture_manager.resume_global_capture()
|
||||||
|
self.capture_manager.activate_fixture()
|
||||||
|
|
Loading…
Reference in New Issue