Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/pytestqt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
7 changes: 6 additions & 1 deletion src/pytestqt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down