From 714c54d7842abdc69eb99803fef552cf651b0dfa Mon Sep 17 00:00:00 2001 From: Arpi-d3y Date: Sat, 24 Oct 2026 16:17:06 -0300 Subject: [PATCH] Fix Qt exception capture configuration and marker precedence --- src/pytestqt/exceptions.py | 11 +++++++---- src/pytestqt/plugin.py | 7 ++++++- tests/test_exceptions.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/pytestqt/exceptions.py b/src/pytestqt/exceptions.py index bdc72da..f30de6c 100644 --- a/src/pytestqt/exceptions.py +++ b/src/pytestqt/exceptions.py @@ -103,10 +103,13 @@ def format_captured_exceptions(exceptions: CapturedExceptions) -> str: def _is_exception_capture_enabled(item: pytest.Item) -> bool: - """returns if exception capture is disabled for the given test item.""" - disabled = get_marker(item, "qt_no_exception_capture") or item.config.getini( - "qt_no_exception_capture" - ) + """Return whether Qt exception capture is enabled for the test item.""" + marker = get_marker(item, "qt_no_exception_capture") + if marker is not None: + # Explicit mark values override the project-wide ini configuration. + disabled = bool(marker.args[0]) if marker.args else True + else: + disabled = item.config.getini("qt_no_exception_capture") return not disabled diff --git a/src/pytestqt/plugin.py b/src/pytestqt/plugin.py index 207351f..ff58e14 100644 --- a/src/pytestqt/plugin.py +++ b/src/pytestqt/plugin.py @@ -128,7 +128,12 @@ def qtmodeltester(request): def pytest_addoption(parser): parser.addini("qt_api", 'Qt api version to use: "pyside6" , "pyqt6", "pyqt5"') - parser.addini("qt_no_exception_capture", "disable automatic exception capture") + parser.addini( + "qt_no_exception_capture", + "disable automatic exception capture", + type="bool", + default=False, + ) parser.addini( "qt_default_raising", "Default value for the raising parameter of qtbot.waitSignal/waitCallback", diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 8c863ea..44aa1e3 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -150,6 +150,40 @@ def test_widget(qtbot): res.stdout.fnmatch_lines(["*1 passed*"]) +@pytest.mark.parametrize( + ("ini_value", "marker_decorator", "expected_enabled"), + [ + ("0", "", True), + ("1", "", False), + ("0", "@pytest.mark.qt_no_exception_capture", False), + ("1", "@pytest.mark.qt_no_exception_capture(False)", True), + ("1", "@pytest.mark.qt_no_exception_capture(0)", True), + ("0", "@pytest.mark.qt_no_exception_capture(True)", False), + ("0", "@pytest.mark.qt_no_exception_capture(1)", False), + ], +) +def test_exception_capture_ini_and_marker_precedence( + pytester: pytest.Pytester, + ini_value: str, + marker_decorator: str, + expected_enabled: bool, +) -> None: + """Explicit marker values override an ini setting parsed as a boolean.""" + pytester.makeini(f"[pytest]\nqt_no_exception_capture = {ini_value}") + pytester.makepyfile( + f""" + import pytest + from pytestqt.exceptions import _is_exception_capture_enabled + + {marker_decorator} + def test_capture_setting(request): + assert _is_exception_capture_enabled(request.node) is {expected_enabled} + """ + ) + result = pytester.runpytest("-q") + result.assert_outcomes(passed=1) + + def test_no_capture_preserves_custom_excepthook(pytester: pytest.Pytester) -> None: """ Capturing must leave custom excepthooks alone when disabled.