Skip to content

Commit 3d6f80d

Browse files
committed
Support for not capturing Qt exceptions using marker or config value
1 parent 53e2de4 commit 3d6f80d

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

pytestqt/_tests/test_exceptions.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import textwrap
21
import pytest
32
import sys
43
from pytestqt.plugin import capture_exceptions, format_captured_exceptions
54
from pytestqt.qt_compat import QtGui, Qt, QtCore
65

76

7+
pytest_plugins = 'pytester'
8+
9+
810
class Receiver(QtCore.QObject):
911
"""
1012
Dummy QObject subclass that raises an error on receiving events if
@@ -45,4 +47,40 @@ def test_format_captured_exceptions():
4547
lines = obtained_text.splitlines()
4648

4749
assert 'Qt exceptions in virtual methods:' in lines
48-
assert 'ValueError: errors were made' in lines
50+
assert 'ValueError: errors were made' in lines
51+
52+
53+
@pytest.mark.parametrize('no_capture_by_marker', [True, False])
54+
def test_no_capture(testdir, no_capture_by_marker):
55+
"""
56+
Make sure options that disable exception capture are working (either marker
57+
or ini configuration value).
58+
:type testdir: TmpTestdir
59+
"""
60+
if no_capture_by_marker:
61+
marker_code = '@pytest.mark.qt_no_exception_capture'
62+
else:
63+
marker_code = ''
64+
testdir.makeini('''
65+
[pytest]
66+
qt_no_exception_capture = 1
67+
''')
68+
testdir.makepyfile('''
69+
import pytest
70+
from pytestqt.qt_compat import QtGui, QtCore
71+
72+
class MyWidget(QtGui.QWidget):
73+
74+
def mouseReleaseEvent(self, ev):
75+
raise RuntimeError
76+
77+
{marker_code}
78+
def test_widget(qtbot):
79+
w = MyWidget()
80+
qtbot.addWidget(w)
81+
qtbot.mouseClick(w, QtCore.Qt.LeftButton)
82+
'''.format(marker_code=marker_code))
83+
result = testdir.runpytest('-s')
84+
# when it fails, it fails with "1 passed, 1 error in", so ensure
85+
# it is passing without errors
86+
result.stdout.fnmatch_lines('*1 passed in*')

pytestqt/plugin.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,34 @@ def qapp():
380380

381381

382382
@pytest.yield_fixture
383-
def qtbot(qapp):
383+
def qtbot(qapp, request):
384384
"""
385385
Fixture used to create a QtBot instance for using during testing.
386386
387387
Make sure to call addWidget for each top-level widget you create to ensure
388388
that they are properly closed after the test ends.
389389
"""
390390
result = QtBot(qapp)
391-
with capture_exceptions() as exceptions:
391+
no_capture = request.node.get_marker('qt_no_exception_capture') or \
392+
request.config.getini('qt_no_exception_capture')
393+
if no_capture:
392394
yield result
393-
394-
if exceptions:
395-
pytest.fail(format_captured_exceptions(exceptions))
395+
else:
396+
with capture_exceptions() as exceptions:
397+
yield result
398+
if exceptions:
399+
pytest.fail(format_captured_exceptions(exceptions))
396400

397401
result._close()
402+
403+
404+
def pytest_addoption(parser):
405+
parser.addini('qt_no_exception_capture',
406+
'disable automatic exception capture')
407+
408+
409+
def pytest_configure(config):
410+
config.addinivalue_line(
411+
'markers',
412+
"qt_no_exception_capture: Disables pytest-qt's automatic exception "
413+
'capture for just one test item.')

0 commit comments

Comments
 (0)