def qtbot(request, qapp, qtbot_params) -> pytest_qt_extras.QtBot: if sys.version_info < (3, 7): pytest.skip('GUI tests are not available for Python 3.6 or lower') _qtbot = pytest_qt_extras.QtBot(request, **qtbot_params) with capture_exceptions() as exceptions: yield _qtbot if exceptions: pytest.fail(format_captured_exceptions(exceptions)) _qtbot.describe()
def assert_raises(exception_type): with capture_exceptions() as exp: yield if not exp: raise AssertionError("No exception raised") elif issubclass(exp[0][0], exception_type): return else: raise AssertionError( f"Assertion type {exp[0][0]} does not match expected type {exception_type}" )
def test_error_raised_if_no_error_handler(qtbot, task_runner): exception = ValueError('value') def _task(): raise exception with capture_exceptions() as exceptions: task_runner.run_task(_task) time.sleep(1) qt_api.QApplication.instance().processEvents() assert len(exceptions) == 1 assert exceptions[0][1] == exception
def _process_events(item): """Calls app.processEvents() while taking care of capturing exceptions or not based on the given item's configuration. """ app = QApplication.instance() if app is not None: if _is_exception_capture_disabled(item): app.processEvents() else: with capture_exceptions() as exceptions: app.processEvents() if exceptions: pytest.fail("TEARDOWN ERROR: " + format_captured_exceptions(exceptions), pytrace=False)
def qtbot(qapp, request): """ Fixture used to create a QtBot instance for using during testing. Make sure to call addWidget for each top-level widget you create to ensure that they are properly closed after the test ends. """ result = QtBot() no_capture = _is_exception_capture_disabled(request.node) if no_capture: yield result # pragma: no cover else: with capture_exceptions() as exceptions: yield result if exceptions: pytest.fail(format_captured_exceptions(exceptions), pytrace=False) result._close()
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.plugin 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.User)) qapp.processEvents() assert [str(e) for (t, e, tb) in exceptions] == ["mistakes were made"]
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.QWidget): def event(self, ev): called.append(1) raise RuntimeError("event processed") w = MyWidget() with capture_exceptions() as exceptions: qapp.postEvent(w, qt_api.QEvent(qt_api.QEvent.User)) qapp.processEvents() assert called del exceptions[:] _out, err = capsys.readouterr() assert 'raise RuntimeError("event processed")' in err
def captureExceptions(self): """ .. versionadded:: 2.1 Context manager that captures Qt virtual method exceptions that happen in block inside context. .. code-block:: python with qtbot.capture_exceptions() as exceptions: qtbot.click(button) # exception is a list of sys.exc_info tuples assert len(exceptions) == 1 .. note:: This method is also available as ``capture_exceptions`` (pep-8 alias) """ from pytestqt.exceptions import capture_exceptions with capture_exceptions() as exceptions: yield exceptions
def qtbot_session(qapp, request): result = QtBot(qapp) with capture_exceptions() as exceptions: yield result if exceptions: pytest.fail(format_captured_exceptions(exceptions))