如何使用exec_()测试名为的自定义对话框窗口?

2022-03-01 00:00:00 python pytest pyqt pyqt5 pytest-qt

问题描述

我正在尝试为我的项目编写系统测试。我有一个启动各种窗口的控制器类。但是,我似乎无法通过qtbot使用exec来控制Windows启动。

以下是MVCE:

from PyQt5.QtWidgets import *
from PyQt5 import QtGui
class Controller:
    def __init__(self):
        self.name = None
        self.a = WindowA(self)

    def launchB(self):
        self.b = WindowB(self)

        if self.b.exec_():
            self.name = self.b.getData()

class WindowA(QDialog):
    def __init__(self, controller):
        super(WindowA, self).__init__()
        self.controller = controller
        layout = QVBoxLayout()
        self.button = QPushButton('Launch B')
        self.button.clicked.connect(self.controller.launchB)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.show()

class WindowB(QDialog):
    def __init__(self, controller):
        super(WindowB, self).__init__()
        self.controller = controller
        layout = QVBoxLayout()
        self.le = QLineEdit()
        self.button = QPushButton('Save')
        self.button.clicked.connect(self.save)
        layout.addWidget(self.le)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.show()

    def getData(self):
        return self.le.text()

    def save(self):
        if self.le.text():
            self.accept()
            self.close()   
        else:
            self.reject()

from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)
    window = Controller()
    sys.exit(app.exec_())

我想测试一下用户是否成功地在lineedit中输入了数据。在我的测试中,我可以成功地单击WindowA中的按钮来启动WindowB,但无法使用按键在lineedit中输入数据。

下面是测试:

def test_1(qtbot):
    control = Controller()
    qtbot.mouseClick(control.a.button, QtCore.Qt.LeftButton)

    qtbot.keyClicks(control.b.le, 'Test_Project')
    qtbot.mouseClick(control.b.button, QtCore.Qt.LeftButton)

    assert control.name == 'Test_Project'

解决方案

问题是使用exec_()会阻塞所有同步任务,直到窗口关闭,解决方案是使用QTimer异步启动剩余任务:

def test_1(qtbot):
    control = Controller()

    def on_timeout():
        qtbot.keyClicks(control.b.le, "Test_Project")
        qtbot.mouseClick(control.b.button, QtCore.Qt.LeftButton)

    QtCore.QTimer.singleShot(0, on_timeout)
    qtbot.mouseClick(control.a.button, QtCore.Qt.LeftButton)

    assert control.name == "Test_Project"

相关文章