1
1
import pytest
2
2
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
5
4
6
5
7
6
pytest_plugins = 'pytester'
8
7
9
8
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 ):
29
11
"""
30
12
Catch exceptions that happen inside Qt virtual methods and make the
31
13
tests fail if any.
14
+
15
+ :type testdir: _pytest.pytester.TmpTestdir
32
16
"""
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*' )
38
45
39
46
40
47
def test_format_captured_exceptions ():
@@ -55,6 +62,7 @@ def test_no_capture(testdir, no_capture_by_marker):
55
62
"""
56
63
Make sure options that disable exception capture are working (either marker
57
64
or ini configuration value).
65
+
58
66
:type testdir: TmpTestdir
59
67
"""
60
68
if no_capture_by_marker :
@@ -80,7 +88,5 @@ def test_widget(qtbot):
80
88
qtbot.addWidget(w)
81
89
qtbot.mouseClick(w, QtCore.Qt.LeftButton)
82
90
''' .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