Skip to content

Understanding PyQt and raising exceptions #25

Closed
@ghost

Description

Hi, I have a few problems understanding the behavior of PyQt and exception handling. Why is no exception raised when zeroDivide() is called by the button click? Is there another solution except to mark the test as expected to fail? Why is the test test_buttonClick2() started two times and test_testFail() just once?

    import pytest
    from pytestqt.qt_compat import QtGui, QtCore

    class TestWidget(QtGui.QWidget):

        def __init__(self, parent=None):
            super(TestWidget, self).__init__(parent)

            self.horizontalLayout = QtGui.QHBoxLayout(self)
            self.button = QtGui.QPushButton("raise exception")
            self.horizontalLayout.addWidget(self.button)
            self.button.clicked.connect(self.zeroDivide)

        def zeroDivide(self):
            0 / 0

    class TestClass(object):

        def setup_class(self):
            self.widget = TestWidget()

        def teardown_class(self):
            pass

        def test_zeroDivide(self):
            with pytest.raises(ZeroDivisionError):
                self.widget.zeroDivide()

        def test_zeroDivide2(self, qtbot):
            qtbot.addWidget(self.widget)
            with pytest.raises(ZeroDivisionError):
                qtbot.mouseClick(self.widget.button, QtCore.Qt.LeftButton)

        @pytest.mark.xfail
        def test_testFail(self):
            assert False

        @pytest.mark.xfail
        def test_buttonClick2(self, qtbot):
            qtbot.addWidget(self.widget)
            qtbot.mouseClick(self.widget.button, QtCore.Qt.LeftButton)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions