Skip to content

Commit 7dca107

Browse files
committed
Refactor test to use testdir instead of failing using xfail
1 parent 46b2e6e commit 7dca107

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

pytestqt/_tests/test_exceptions.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
import pytest
22
import sys
3-
from pytestqt.plugin import capture_exceptions, format_captured_exceptions
4-
from pytestqt.qt_compat import QtGui, Qt, QtCore, QApplication
3+
from pytestqt.plugin import format_captured_exceptions
54

65

76
pytest_plugins = 'pytester'
87

98

10-
class Receiver(QtCore.QObject):
11-
"""
12-
Dummy QObject subclass that raises an error on receiving events if
13-
`raise_error` is True.
14-
"""
15-
16-
def __init__(self, raise_error, *args, **kwargs):
17-
QtCore.QObject.__init__(self, *args, **kwargs)
18-
self._raise_error = raise_error
19-
20-
21-
def event(self, ev):
22-
if self._raise_error:
23-
raise ValueError('mistakes were made')
24-
return QtCore.QObject.event(self, ev)
25-
26-
27-
@pytest.mark.parametrize('raise_error', [False, pytest.mark.xfail(True)])
28-
def test_catch_exceptions_in_virtual_methods(qtbot, raise_error):
9+
@pytest.mark.parametrize('raise_error', [False, True])
10+
def test_catch_exceptions_in_virtual_methods(testdir, raise_error):
2911
"""
3012
Catch exceptions that happen inside Qt virtual methods and make the
3113
tests fail if any.
14+
15+
:type testdir: _pytest.pytester.TmpTestdir
3216
"""
33-
v = Receiver(raise_error)
34-
app = QApplication.instance()
35-
app.sendEvent(v, QtCore.QEvent(QtCore.QEvent.User))
36-
app.sendEvent(v, QtCore.QEvent(QtCore.QEvent.User))
37-
app.processEvents()
17+
testdir.makepyfile('''
18+
from pytestqt.qt_compat import QtCore, QApplication
19+
20+
class Receiver(QtCore.QObject):
21+
22+
def event(self, ev):
23+
if {raise_error}:
24+
raise ValueError('mistakes were made')
25+
return QtCore.QObject.event(self, ev)
26+
27+
28+
def test_exceptions(qtbot):
29+
v = Receiver()
30+
app = QApplication.instance()
31+
app.sendEvent(v, QtCore.QEvent(QtCore.QEvent.User))
32+
app.sendEvent(v, QtCore.QEvent(QtCore.QEvent.User))
33+
app.processEvents()
34+
35+
'''.format(raise_error=raise_error))
36+
result = testdir.runpytest()
37+
if raise_error:
38+
result.stdout.fnmatch_lines([
39+
'*Qt exceptions in virtual methods:*',
40+
'*ValueError: mistakes were made*',
41+
'*1 error*',
42+
])
43+
else:
44+
result.stdout.fnmatch_lines('*1 passed*')
3845

3946

4047
def test_format_captured_exceptions():
@@ -55,6 +62,7 @@ def test_no_capture(testdir, no_capture_by_marker):
5562
"""
5663
Make sure options that disable exception capture are working (either marker
5764
or ini configuration value).
65+
5866
:type testdir: TmpTestdir
5967
"""
6068
if no_capture_by_marker:
@@ -80,7 +88,5 @@ def test_widget(qtbot):
8088
qtbot.addWidget(w)
8189
qtbot.mouseClick(w, QtCore.Qt.LeftButton)
8290
'''.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*')
91+
res = testdir.inline_run()
92+
res.assertoutcome(passed=1)

0 commit comments

Comments
 (0)