Skip to content

Commit

Permalink
Use new-style hook wrappers (#542)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicoddemus committed Feb 7, 2024
1 parent 4d099f5 commit bd3469b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
UNRELEASED
----------

- ``pluggy >=1.1`` is now required: we now use new-style hook wrappers, which are less error prone.

- 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`_).

.. _#532: https://github.com/pytest-dev/pytest-qt/issues/532
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
packages=find_packages(where="src"),
package_dir={"": "src"},
entry_points={"pytest11": ["pytest-qt = pytestqt.plugin"]},
install_requires=["pytest>=3.0.0"],
install_requires=["pytest>=3.0.0", "pluggy>=1.1"],
extras_require={
"doc": ["sphinx", "sphinx_rtd_theme"],
"dev": ["pre-commit", "tox"],
Expand Down
9 changes: 4 additions & 5 deletions src/pytestqt/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ def pytest_runtest_setup(self, item):
item.qt_log_capture = _QtMessageCapture(ignore_regexes)
item.qt_log_capture._start()

@pytest.hookimpl(hookwrapper=True)
@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(self, item, call):
"""Add captured Qt messages to test item report if the call failed."""
outcome = yield
report = yield
if not hasattr(item, "qt_log_capture"):
return
return report

if call.when == "call":
report = outcome.get_result()

m = get_marker(item, "qt_log_level_fail")
if m:
log_fail_level = m.args[0]
Expand Down Expand Up @@ -111,6 +109,7 @@ def pytest_runtest_makereport(self, item, call):

item.qt_log_capture._stop()
del item.qt_log_capture
return report


class _QtMessageCapture:
Expand Down
15 changes: 9 additions & 6 deletions src/pytestqt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def pytest_addoption(parser):
)


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_setup(item):
"""
Hook called after before test setup starts, to start capturing exceptions
Expand All @@ -177,22 +177,24 @@ def pytest_runtest_setup(item):
if capture_enabled:
item.qt_exception_capture_manager = _QtExceptionCaptureManager()
item.qt_exception_capture_manager.start()
yield
result = yield
_process_events()
if capture_enabled:
item.qt_exception_capture_manager.fail_if_exceptions_occurred("SETUP")
return result


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_call(item):
yield
result = yield
_process_events()
capture_enabled = _is_exception_capture_enabled(item)
if capture_enabled:
item.qt_exception_capture_manager.fail_if_exceptions_occurred("CALL")
return result


@pytest.hookimpl(hookwrapper=True, trylast=True)
@pytest.hookimpl(wrapper=True, trylast=True)
def pytest_runtest_teardown(item):
"""
Hook called after each test tear down, to process any pending events and
Expand All @@ -202,12 +204,13 @@ def pytest_runtest_teardown(item):
_process_events()
_close_widgets(item)
_process_events()
yield
result = yield
_process_events()
capture_enabled = _is_exception_capture_enabled(item)
if capture_enabled:
item.qt_exception_capture_manager.fail_if_exceptions_occurred("TEARDOWN")
item.qt_exception_capture_manager.finish()
return result


def _process_events():
Expand Down

0 comments on commit bd3469b

Please sign in to comment.