1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
import sys
import pytest
from pytestqt.exceptions import capture_exceptions, format_captured_exceptions
@pytest.mark.parametrize("raise_error", [False, True])
def test_catch_exceptions_in_virtual_methods(testdir, raise_error):
"""
Catch exceptions that happen inside Qt's event loop and make the
tests fail if any.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
class Receiver(qt_api.QtCore.QObject):
def event(self, ev):
if {raise_error}:
try:
raise RuntimeError('original error')
except RuntimeError:
raise ValueError('mistakes were made')
return qt_api.QtCore.QObject.event(self, ev)
def test_exceptions(qtbot):
v = Receiver()
app = qt_api.QtWidgets.QApplication.instance()
app.sendEvent(v, qt_api.QtCore.QEvent(qt_api.QtCore.QEvent.Type.User))
app.sendEvent(v, qt_api.QtCore.QEvent(qt_api.QtCore.QEvent.Type.User))
app.processEvents()
""".format(
raise_error=raise_error
)
)
result = testdir.runpytest()
if raise_error:
expected_lines = ["*Exceptions caught in Qt event loop:*"]
if sys.version_info.major == 3:
expected_lines.append("RuntimeError: original error")
expected_lines.extend(["*ValueError: mistakes were made*", "*1 failed*"])
result.stdout.fnmatch_lines(expected_lines)
assert "pytest.fail" not in "\n".join(result.outlines)
else:
result.stdout.fnmatch_lines("*1 passed*")
def test_format_captured_exceptions():
try:
raise ValueError("errors were made")
except ValueError:
exceptions = [sys.exc_info()]
obtained_text = format_captured_exceptions(exceptions)
lines = obtained_text.splitlines()
assert "Exceptions caught in Qt event loop:" in lines
assert "ValueError: errors were made" in lines
@pytest.mark.skipif(sys.version_info.major == 2, reason="Python 3 only")
def test_format_captured_exceptions_chained():
try:
try:
raise ValueError("errors were made")
except ValueError:
raise RuntimeError("error handling value error")
except RuntimeError:
exceptions = [sys.exc_info()]
obtained_text = format_captured_exceptions(exceptions)
lines = obtained_text.splitlines()
assert "Exceptions caught in Qt event loop:" in lines
assert "ValueError: errors were made" in lines
assert "RuntimeError: error handling value error" in lines
@pytest.mark.parametrize("no_capture_by_marker", [True, False])
def test_no_capture(testdir, no_capture_by_marker):
"""
Make sure options that disable exception capture are working (either marker
or ini configuration value).
:type testdir: TmpTestdir
"""
if no_capture_by_marker:
marker_code = "@pytest.mark.qt_no_exception_capture"
else:
marker_code = ""
testdir.makeini(
"""
[pytest]
qt_no_exception_capture = 1
"""
)
testdir.makepyfile(
"""
import pytest
import sys
from pytestqt.qt_compat import qt_api
# PyQt 5.5+ will crash if there's no custom exception handler installed
sys.excepthook = lambda *args: None
class MyWidget(qt_api.QtWidgets.QWidget):
def mouseReleaseEvent(self, ev):
raise RuntimeError
{marker_code}
def test_widget(qtbot):
w = MyWidget()
qtbot.addWidget(w)
qtbot.mouseClick(w, qt_api.QtCore.Qt.MouseButton.LeftButton)
""".format(
marker_code=marker_code
)
)
res = testdir.runpytest()
res.stdout.fnmatch_lines(["*1 passed*"])
def test_no_capture_preserves_custom_excepthook(testdir):
"""
Capturing must leave custom excepthooks alone when disabled.
:type testdir: TmpTestdir
"""
testdir.makepyfile(
"""
import pytest
import sys
from pytestqt.qt_compat import qt_api
def custom_excepthook(*args):
sys.__excepthook__(*args)
sys.excepthook = custom_excepthook
@pytest.mark.qt_no_exception_capture
def test_no_capture(qtbot):
assert sys.excepthook is custom_excepthook
def test_capture(qtbot):
assert sys.excepthook is not custom_excepthook
"""
)
res = testdir.runpytest()
res.stdout.fnmatch_lines(["*2 passed*"])
def test_exception_capture_on_call(testdir):
"""
Exceptions should also be captured during test execution.
:type testdir: TmpTestdir
"""
testdir.makepyfile(
"""
import pytest
from pytestqt.qt_compat import qt_api
class MyWidget(qt_api.QtWidgets.QWidget):
def event(self, ev):
raise RuntimeError('event processed')
def test_widget(qtbot, qapp):
w = MyWidget()
qapp.postEvent(w, qt_api.QtCore.QEvent(QEvent.Type.User))
qapp.processEvents()
"""
)
res = testdir.runpytest("-s")
res.stdout.fnmatch_lines(["*RuntimeError('event processed')*", "*1 failed*"])
def test_exception_capture_on_widget_close(testdir):
"""
Exceptions should also be captured when widget is being closed.
:type testdir: TmpTestdir
"""
testdir.makepyfile(
"""
import pytest
from pytestqt.qt_compat import qt_api
class MyWidget(qt_api.QtWidgets.QWidget):
def closeEvent(self, ev):
raise RuntimeError('close error')
def test_widget(qtbot, qapp):
w = MyWidget()
test_widget.w = w # keep it alive
qtbot.addWidget(w)
"""
)
res = testdir.runpytest("-s")
res.stdout.fnmatch_lines(["*RuntimeError('close error')*", "*1 error*"])
@pytest.mark.parametrize("mode", ["setup", "teardown"])
def test_exception_capture_on_fixture_setup_and_teardown(testdir, mode):
"""
Setup/teardown exception capturing as early/late as possible to catch
all exceptions, even from other fixtures (#105).
:type testdir: TmpTestdir
"""
if mode == "setup":
setup_code = "send_event(w, qapp)"
teardown_code = ""
else:
setup_code = ""
teardown_code = "send_event(w, qapp)"
testdir.makepyfile(
"""
import pytest
from pytestqt.qt_compat import qt_api
class MyWidget(qt_api.QtWidgets.QWidget):
def event(self, ev):
if ev.type() == qt_api.QtCore.QEvent.Type.User:
raise RuntimeError('event processed')
return True
@pytest.fixture
def widget(qapp):
w = MyWidget()
{setup_code}
yield w
{teardown_code}
def send_event(w, qapp):
qapp.postEvent(w, qt_api.QtCore.QEvent(
qt_api.QtCore.QEvent.Type.User))
qapp.processEvents()
def test_capture(widget):
pass
""".format(
setup_code=setup_code, teardown_code=teardown_code
)
)
res = testdir.runpytest("-s")
res.stdout.fnmatch_lines(
[
"*__ ERROR at %s of test_capture __*" % mode,
"*RuntimeError('event processed')*",
"*1 error*",
]
)
@pytest.mark.qt_no_exception_capture
def test_capture_exceptions_context_manager(qapp):
"""Test capture_exceptions() context manager.
While not used internally anymore, it is still part of the API and therefore
should be properly tested.
"""
from pytestqt.qt_compat import qt_api
from pytestqt.exceptions import capture_exceptions
class Receiver(qt_api.QtCore.QObject):
def event(self, ev):
raise ValueError("mistakes were made")
r = Receiver()
with capture_exceptions() as exceptions:
qapp.sendEvent(r, qt_api.QtCore.QEvent(qt_api.QtCore.QEvent.Type.User))
qapp.processEvents()
assert [str(e) for (t, e, tb) in exceptions] == ["mistakes were made"]
def test_capture_exceptions_qtbot_context_manager(testdir):
"""Test capturing exceptions in a block by using `capture_exceptions` method provided
by `qtbot`.
"""
testdir.makepyfile(
"""
import pytest
from pytestqt.qt_compat import qt_api
class MyWidget(qt_api.QtWidgets.QWidget):
on_event = qt_api.Signal()
def test_widget(qtbot):
widget = MyWidget()
qtbot.addWidget(widget)
def raise_on_event():
raise RuntimeError("error")
widget.on_event.connect(raise_on_event)
with qtbot.capture_exceptions() as exceptions:
widget.on_event.emit()
assert len(exceptions) == 1
assert str(exceptions[0][1]) == "error"
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
def test_exceptions_to_stderr(qapp, capsys):
"""
Exceptions should still be reported to stderr.
"""
called = []
from pytestqt.qt_compat import qt_api
class MyWidget(qt_api.QtWidgets.QWidget):
def event(self, ev):
called.append(1)
raise RuntimeError("event processed")
w = MyWidget()
with capture_exceptions() as exceptions:
qapp.postEvent(w, qt_api.QtCore.QEvent(qt_api.QtCore.QEvent.Type.User))
qapp.processEvents()
assert called
del exceptions[:]
_out, err = capsys.readouterr()
assert 'raise RuntimeError("event processed")' in err
@pytest.mark.xfail(
condition=sys.version_info[:2] == (3, 4),
reason="failing in Python 3.4, which is about to be dropped soon anyway",
)
def test_exceptions_dont_leak(testdir):
"""
Ensure exceptions are cleared when an exception occurs and don't leak (#187).
"""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import gc
import weakref
class MyWidget(qt_api.QtWidgets.QWidget):
def event(self, ev):
called.append(1)
raise RuntimeError('event processed')
weak_ref = None
called = []
def test_1(qapp):
global weak_ref
w = MyWidget()
weak_ref = weakref.ref(w)
qapp.postEvent(w, qt_api.QtCore.QEvent(qt_api.QtCore.QEvent.Type.User))
qapp.processEvents()
def test_2(qapp):
assert called
gc.collect()
assert weak_ref() is None
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 failed, 1 passed*"])
|