Dialog button click in test not dismissing dialog PyQt5/pytest

huangapple go评论47阅读模式
英文:

Dialog button click in test not dismissing dialog PyQt5/pytest

问题

在运行一个启动对话框的测试时,测试无法以编程方式单击对话框上的按钮。如果我手动单击按钮,测试就会完成。此外,我不确定为什么对话框会显示(因为我看不到它是从哪个原始窗口启动的)。

Python 3.7.16,pytest 7.2.1

foo.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Foo(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        
        self.setupUi()

    def setupUi(self):
        self.resize(400, 200)
        
        self.button = QtWidgets.QPushButton("ClickMe")
        self.button.clicked.connect(self.launchDialog)
        
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        
    def launchDialog(self):
        self.dialog = FooDialog(parent=self)
        self.dialog.exec()

class FooDialog(QtWidgets.QMessageBox):
    def __init__(self, parent=None):
        super(FooDialog, self).__init__(parent)
        
        self.setWindowTitle("foo.FooDialog")
        
        self.button = QtWidgets.QPushButton("ClickMe")
        self.addButton(self.button, QtWidgets.QMessageBox.ButtonRole.ActionRole)
        

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    foo = Foo()
    foo.show()
    sys.exit(app.exec_())

test_foo.py

import pytest, time
from pytestqt.qtbot import QtBot
from PyQt5 import QtCore

from foo import Foo


@pytest.fixture
def app(qtbot):
    foo = Foo()
    qtbot.addWidget(foo)
    return foo


def test_button(app):
    qtbot = QtBot(app)
    qtbot.mouseClick(app.button, QtCore.Qt.MouseButton.LeftButton, delay=0)
    time.sleep(2)
    qtbot.mouseClick(app.dialog.button, QtCore.Qt.MouseButton.LeftButton, delay=0)

我运行测试如下:

pytest -s test_foo.py

我运行了测试并期望它完成,但实际上测试挂起等待对话框上的按钮被点击。

英文:

When running a test that launches a dialog, the test is unable to programmatically click a button on the dialog. If I manually click the button, the test completes. Also I'm unsure why the dialog even shows at all (since I don't see the original window it was launched from).

Python 3.7.16, pytest 7.2.1

foo.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Foo(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        
        self.setupUi()

    def setupUi(self):
        self.resize(400, 200)
        
        self.button = QtWidgets.QPushButton("ClickMe")
        self.button.clicked.connect(self.launchDialog)
        
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        
    def launchDialog(self):
        self.dialog = FooDialog(parent=self)
        self.dialog.exec()

class FooDialog(QtWidgets.QMessageBox):
    def __init__(self, parent=None):
        super(FooDialog, self).__init__(parent)
        
        self.setWindowTitle("foo.FooDialog")
        
        self.button = QtWidgets.QPushButton("ClickMe")
        self.addButton(self.button, QtWidgets.QMessageBox.ButtonRole.ActionRole)
        

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    foo = Foo()
    foo.show()
    sys.exit(app.exec_())

test_foo.py

import pytest, time
from pytestqt.qtbot import QtBot
from PyQt5 import QtCore

from foo import Foo


@pytest.fixture
def app(qtbot):
    foo = Foo()
    qtbot.addWidget(foo)
    return foo


def test_button(app):
    qtbot = QtBot(app)
    qtbot.mouseClick(app.button, QtCore.Qt.MouseButton.LeftButton, delay=0)
    time.sleep(2)
    qtbot.mouseClick(app.dialog.button, QtCore.Qt.MouseButton.LeftButton, delay=0)

I run the test thus:

pytest -s test_foo.py

I ran the test and expected it to complete, but instead the test hangs waiting for the button on the dialog to be clicked.

答案1

得分: 0

我通过模拟对话来解决了这个问题。

英文:

I worked around the problem by mocking the dialog

huangapple
  • 本文由 发表于 2023年2月7日 03:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定