QMessageBox 的功能测试...为什么不起作用? [英] Functional Test for QMessageBox... why does not work?

查看:63
本文介绍了QMessageBox 的功能测试...为什么不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将为使用 PyQt(或 PySide)作为 GUI 库的 pyqt 应用程序开发一些功能测试.测试使用 Unittest 和 Qttest 库,如许多资源中所述,例如这个 stackoverflow 问题:对基于 PySide 的应用程序进行单元和功能测试?对于主窗口,一切正常,代码完美地模拟了键盘类型和鼠标点击和移动,但是魔鬼在细节中"...并且此方法不适用于 QMessageBox.

I would develop some functional tests for a pyqt application that uses PyQt (or PySide) as GUI library. The tests use Unittest and Qttest library, as reported in many resources, for example this stackoverflow question: Unit and functional testing a PySide-based application? For the main window all works fine, and the code simulate perfectly Keyboard Types and Mouse Clicks and Movements, but the "devil is in the details"... and this method does not work for a QMessageBox.

在主窗口的类中,为了管理打开文件时的IOError,我初始化了一个QMessageBox:

In the class of the Main Window, for manage a IOError on opening a file, I initialize a QMessageBox:

self.IOErrMsgBox = QtGui.QMessageBox()
self.IOErrMsgBox.setText("<b>Error</b>")
self.IOErrMsgBox.setInformativeText("""
                                    <p>There was an error opening
                                    the project file:
                                    %s.</p>"""%(path,))
self.IOErrMsgBox.setStandardButtons(QtGui.QMessageBox.Ok)
self.IOErrMsgBox.setDefaultButton(QtGui.QMessageBox.Ok)
self.IOErrMsgBox.exec_()

为了测试它是如何工作的,在功能测试中我有:

To test how it works, in functional test I have:

def test__open_project(self):
    self.MainWin._project_open(wrong_path, flag='c') 
    # the function that handles the exception 
    # and initializes the QMessageBox.
    IOErrMsgBox = self.MainWin.IOErrMsgBox
    # Reference to the initialized QMessageBox.
    self.assertIsInstance(IOErrMsgBox, QMessageBox)
    okWidget = self.MainWin.IOErrMsgBox.button(IOErrMsgBox.Ok)
    QTest.mouseClick(okWidget, Qt.LeftButton)

或者,替代:

def test__open_project(self):
     #... some code, exactly like previous example except for last row...
     QTest.keyClick(okWidget, 'o', Qt.AltModifier)

但是没有人工作......并且没有点击确定按钮,我可以用鼠标指针来做到这一点:(

but No one works... and the Ok button is not clicked and I can do it with my mouse pointer :(

有什么建议吗?

推荐答案

问题通常是关于如何测试模态对话框.

The question is in general about how to test modal dialogs.

包括 QMessageBox 在内的任何模态对话框在关闭之前都不会从 exec_() 返回,因此您的第二个代码框中的测试代码可能永远不会被执行.

Any modal dialog including QMessageBox will not return from exec_() until it is closed, so the test code in your second code box probably never gets executed.

您可以只 show() 它(使其成为非模态),然后按照您的代码进行操作,但不要忘记关闭并删除对话框.

You could just show() it (making it non-modal) and then follow your code but don't forget to close and delete the dialog afterwards.

或者您使用计时器并安排单击确定"按钮(类似于 测试带有 Qt 测试的模态对话框).下面是一个例子:

Or you use a Timer and schedule a click on the OK button (similar to Test modal dialog with Qt Test). Here is an example:

from PySide import QtGui, QtCore

app = QtGui.QApplication([])

box = QtGui.QMessageBox()
box.setStandardButtons(QtGui.QMessageBox.Ok)
button = box.button(QtGui.QMessageBox.Ok)
QtCore.QTimer.singleShot(0, button.clicked)
box.exec_()

这篇关于QMessageBox 的功能测试...为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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