Skip to content

Commit bd3469b

Browse files
authored
Use new-style hook wrappers (#542)
The [new-style hook wrappers](https://pluggy.readthedocs.io/en/stable/#wrappers) are easier to use because you do not need to call `outcome.force_exception` on errors, and doing so prevents other hook callers from running. This requires pluggy>=1.1.
1 parent 4d099f5 commit bd3469b

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
UNRELEASED
22
----------
33

4+
- ``pluggy >=1.1`` is now required: we now use new-style hook wrappers, which are less error prone.
5+
46
- Fixed exception handling so they are properly cleared in Python 3.12, due to the new `sys.last_exc <https://docs.python.org/3/library/sys.html#sys.last_exc>`__ attribute (`#532`_).
57

68
.. _#532: https://github.com/pytest-dev/pytest-qt/issues/532

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
packages=find_packages(where="src"),
99
package_dir={"": "src"},
1010
entry_points={"pytest11": ["pytest-qt = pytestqt.plugin"]},
11-
install_requires=["pytest>=3.0.0"],
11+
install_requires=["pytest>=3.0.0", "pluggy>=1.1"],
1212
extras_require={
1313
"doc": ["sphinx", "sphinx_rtd_theme"],
1414
"dev": ["pre-commit", "tox"],

src/pytestqt/logging.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ def pytest_runtest_setup(self, item):
3939
item.qt_log_capture = _QtMessageCapture(ignore_regexes)
4040
item.qt_log_capture._start()
4141

42-
@pytest.hookimpl(hookwrapper=True)
42+
@pytest.hookimpl(wrapper=True)
4343
def pytest_runtest_makereport(self, item, call):
4444
"""Add captured Qt messages to test item report if the call failed."""
45-
outcome = yield
45+
report = yield
4646
if not hasattr(item, "qt_log_capture"):
47-
return
47+
return report
4848

4949
if call.when == "call":
50-
report = outcome.get_result()
51-
5250
m = get_marker(item, "qt_log_level_fail")
5351
if m:
5452
log_fail_level = m.args[0]
@@ -111,6 +109,7 @@ def pytest_runtest_makereport(self, item, call):
111109

112110
item.qt_log_capture._stop()
113111
del item.qt_log_capture
112+
return report
114113

115114

116115
class _QtMessageCapture:

src/pytestqt/plugin.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def pytest_addoption(parser):
167167
)
168168

169169

170-
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
170+
@pytest.hookimpl(wrapper=True, tryfirst=True)
171171
def pytest_runtest_setup(item):
172172
"""
173173
Hook called after before test setup starts, to start capturing exceptions
@@ -177,22 +177,24 @@ def pytest_runtest_setup(item):
177177
if capture_enabled:
178178
item.qt_exception_capture_manager = _QtExceptionCaptureManager()
179179
item.qt_exception_capture_manager.start()
180-
yield
180+
result = yield
181181
_process_events()
182182
if capture_enabled:
183183
item.qt_exception_capture_manager.fail_if_exceptions_occurred("SETUP")
184+
return result
184185

185186

186-
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
187+
@pytest.hookimpl(wrapper=True, tryfirst=True)
187188
def pytest_runtest_call(item):
188-
yield
189+
result = yield
189190
_process_events()
190191
capture_enabled = _is_exception_capture_enabled(item)
191192
if capture_enabled:
192193
item.qt_exception_capture_manager.fail_if_exceptions_occurred("CALL")
194+
return result
193195

194196

195-
@pytest.hookimpl(hookwrapper=True, trylast=True)
197+
@pytest.hookimpl(wrapper=True, trylast=True)
196198
def pytest_runtest_teardown(item):
197199
"""
198200
Hook called after each test tear down, to process any pending events and
@@ -202,12 +204,13 @@ def pytest_runtest_teardown(item):
202204
_process_events()
203205
_close_widgets(item)
204206
_process_events()
205-
yield
207+
result = yield
206208
_process_events()
207209
capture_enabled = _is_exception_capture_enabled(item)
208210
if capture_enabled:
209211
item.qt_exception_capture_manager.fail_if_exceptions_occurred("TEARDOWN")
210212
item.qt_exception_capture_manager.finish()
213+
return result
211214

212215

213216
def _process_events():

0 commit comments

Comments
 (0)