如何使用exec_()测试名为的自定义对话框窗口? [英] How to test a custom dialog window called using exec_()?

查看:35
本文介绍了如何使用exec_()测试名为的自定义对话框窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目编写系统测试。我有一个启动各种窗口的控制器类。但是,我似乎无法通过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"

这篇关于如何使用exec_()测试名为的自定义对话框窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆