|
1 | 1 | import pytest
|
2 | 2 | import sys
|
3 |
| -from pytestqt.plugin import format_captured_exceptions |
| 3 | +from pytestqt.plugin import format_captured_exceptions, QT_API |
4 | 4 |
|
5 | 5 |
|
6 | 6 | pytest_plugins = 'pytester'
|
@@ -58,6 +58,8 @@ def test_format_captured_exceptions():
|
58 | 58 |
|
59 | 59 |
|
60 | 60 | @pytest.mark.parametrize('no_capture_by_marker', [True, False])
|
| 61 | +@pytest.mark.skipif(QT_API == 'pyqt5', reason='non captured exceptions on PyQt' |
| 62 | + ' 5.5+ crash the interpreter.') |
61 | 63 | def test_no_capture(testdir, no_capture_by_marker):
|
62 | 64 | """
|
63 | 65 | Make sure options that disable exception capture are working (either marker
|
@@ -90,3 +92,34 @@ def test_widget(qtbot):
|
90 | 92 | '''.format(marker_code=marker_code))
|
91 | 93 | res = testdir.inline_run()
|
92 | 94 | res.assertoutcome(passed=1)
|
| 95 | + |
| 96 | + |
| 97 | +def test_exception_capture_on_teardown(testdir): |
| 98 | + """ |
| 99 | + Exceptions should also be captured during test teardown. |
| 100 | +
|
| 101 | + :type testdir: TmpTestdir |
| 102 | + """ |
| 103 | + testdir.makepyfile(''' |
| 104 | + import pytest |
| 105 | + from pytestqt.qt_compat import QWidget, QtCore, QEvent |
| 106 | +
|
| 107 | + class MyWidget(QWidget): |
| 108 | +
|
| 109 | + def event(self, ev): |
| 110 | + raise RuntimeError('event processed') |
| 111 | +
|
| 112 | + def test_widget(qtbot, qapp): |
| 113 | + w = MyWidget() |
| 114 | + # keep a reference to the widget so it will lives after the test |
| 115 | + # ends. This will in turn trigger its event() during test tear down, |
| 116 | + # raising the exception during its event processing |
| 117 | + test_widget.w = w |
| 118 | + qapp.postEvent(w, QEvent(QEvent.User)) |
| 119 | + ''') |
| 120 | + res = testdir.runpytest('-s') |
| 121 | + res.stdout.fnmatch_lines([ |
| 122 | + "*RuntimeError('event processed')*", |
| 123 | + '*1 error*', |
| 124 | + ]) |
| 125 | + |
0 commit comments