3
3
import pytest
4
4
5
5
from pytestqt .exceptions import capture_exceptions , format_captured_exceptions
6
+ from pytestqt .qt_compat import qt_api
7
+
8
+ # PySide6 is automatically captures exceptions during the event loop,
9
+ # and re-raises them when control gets back to Python, so the related
10
+ # functionality does not work, nor is needed for the end user.
11
+ exception_capture_pyside6 = pytest .mark .skipif (
12
+ qt_api .pytest_qt_api == "pyside6" ,
13
+ reason = "pytest-qt capture not working/needed on PySide6" ,
14
+ )
6
15
7
16
8
17
@pytest .mark .parametrize ("raise_error" , [False , True ])
@@ -42,10 +51,24 @@ def test_exceptions(qtbot):
42
51
)
43
52
result = testdir .runpytest ()
44
53
if raise_error :
45
- expected_lines = ["*Exceptions caught in Qt event loop:*" ]
46
- if sys .version_info .major == 3 :
47
- expected_lines .append ("RuntimeError: original error" )
48
- expected_lines .extend (["*ValueError: mistakes were made*" , "*1 failed*" ])
54
+ if qt_api .pytest_qt_api == "pyside6" :
55
+ # PySide6 automatically captures exceptions during the event loop,
56
+ # and re-raises them when control gets back to Python.
57
+ # This results in the exception not being captured by
58
+ # us, and a more natural traceback which includes the app.sendEvent line.
59
+ expected_lines = [
60
+ "*RuntimeError: original error" ,
61
+ "*app.sendEvent*" ,
62
+ "*ValueError: mistakes were made*" ,
63
+ "*1 failed*" ,
64
+ ]
65
+ else :
66
+ expected_lines = [
67
+ "*Exceptions caught in Qt event loop:*" ,
68
+ "RuntimeError: original error" ,
69
+ "*ValueError: mistakes were made*" ,
70
+ "*1 failed*" ,
71
+ ]
49
72
result .stdout .fnmatch_lines (expected_lines )
50
73
assert "pytest.fail" not in "\n " .join (result .outlines )
51
74
else :
@@ -65,7 +88,6 @@ def test_format_captured_exceptions():
65
88
assert "ValueError: errors were made" in lines
66
89
67
90
68
- @pytest .mark .skipif (sys .version_info .major == 2 , reason = "Python 3 only" )
69
91
def test_format_captured_exceptions_chained ():
70
92
try :
71
93
try :
@@ -84,6 +106,7 @@ def test_format_captured_exceptions_chained():
84
106
85
107
86
108
@pytest .mark .parametrize ("no_capture_by_marker" , [True , False ])
109
+ @exception_capture_pyside6
87
110
def test_no_capture (testdir , no_capture_by_marker ):
88
111
"""
89
112
Make sure options that disable exception capture are working (either marker
@@ -99,15 +122,15 @@ def test_no_capture(testdir, no_capture_by_marker):
99
122
"""
100
123
[pytest]
101
124
qt_no_exception_capture = 1
102
- """
125
+ """
103
126
)
104
127
testdir .makepyfile (
105
- """
128
+ f """
106
129
import pytest
107
130
import sys
108
131
from pytestqt.qt_compat import qt_api
109
132
110
- # PyQt 5.5+ will crash if there's no custom exception handler installed
133
+ # PyQt 5.5+ will crash if there's no custom exception handler installed.
111
134
sys.excepthook = lambda *args: None
112
135
113
136
class MyWidget(qt_api.QtWidgets.QWidget):
@@ -120,9 +143,7 @@ def test_widget(qtbot):
120
143
w = MyWidget()
121
144
qtbot.addWidget(w)
122
145
qtbot.mouseClick(w, qt_api.QtCore.Qt.MouseButton.LeftButton)
123
- """ .format (
124
- marker_code = marker_code
125
- )
146
+ """
126
147
)
127
148
res = testdir .runpytest ()
128
149
res .stdout .fnmatch_lines (["*1 passed*" ])
@@ -265,6 +286,7 @@ def test_capture(widget):
265
286
266
287
267
288
@pytest .mark .qt_no_exception_capture
289
+ @exception_capture_pyside6
268
290
def test_capture_exceptions_context_manager (qapp ):
269
291
"""Test capture_exceptions() context manager.
270
292
@@ -319,6 +341,7 @@ def raise_on_event():
319
341
result .stdout .fnmatch_lines (["*1 passed*" ])
320
342
321
343
344
+ @exception_capture_pyside6
322
345
def test_exceptions_to_stderr (qapp , capsys ):
323
346
"""
324
347
Exceptions should still be reported to stderr.
@@ -341,10 +364,7 @@ def event(self, ev):
341
364
assert 'raise RuntimeError("event processed")' in err
342
365
343
366
344
- @pytest .mark .xfail (
345
- condition = sys .version_info [:2 ] == (3 , 4 ),
346
- reason = "failing in Python 3.4, which is about to be dropped soon anyway" ,
347
- )
367
+ @exception_capture_pyside6
348
368
def test_exceptions_dont_leak (testdir ):
349
369
"""
350
370
Ensure exceptions are cleared when an exception occurs and don't leak (#187).
0 commit comments