python - pytest-qt测试对话框

标签 python pyqt pyqt5 pytest

我试图建立一个对话框并为此编写一个测试用例。

这是我的对话框代码-

import sys
import os.path as osp
from qtpy import QT_VERSION
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets

QT5 = QT_VERSION[0] == '5'  # NOQA

here = osp.dirname(osp.abspath(__file__))

def newIcon(icon):
    icons_dir = osp.join(here, 'icons')
    return QtGui.QIcon(osp.join(':/', icons_dir, '%s.png' % icon))

class Example(QtWidgets.QMainWindow):
    count = 0
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)            
        self.meta = MetaDialog()            
        self.meta.popUp()            

class MetaDialog(QtWidgets.QDialog):

    def __init__(self, parent=None,):
        super(MetaDialog, self).__init__(parent)
        self.parent = parent
        self.setWindowTitle('Meta information')                    
        self.street1 = QtWidgets.QLabel('street1')
        self.street2 = QtWidgets.QLabel('street2')

        self.street1_edit = QtWidgets.QLineEdit()
        self.street2_edit = QtWidgets.QLineEdit()

        grid = QtWidgets.QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(self.street1, 0, 0)
        grid.addWidget(self.street1_edit, 0, 1)        
        grid.addWidget(self.street2, 1, 0)
        grid.addWidget(self.street2_edit, 1, 1)                                
        # buttons
        self.buttonBox = bb = QtWidgets.QDialogButtonBox(
            QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel,
            QtCore.Qt.Horizontal,            
            self,
        )
        bb.button(bb.Ok).setIcon(newIcon('done'))
        bb.button(bb.Cancel).setIcon(newIcon('undo'))
        bb.accepted.connect(self.validate)
        bb.rejected.connect(self.reject)
        grid.addWidget(bb, 11,0)
        self.setLayout(grid)

    def validate(self):
        self.accept()

    def popUp(self, meta=None):

        return (self.street1_edit.text(),
                self.street2_edit.text(),                
                )  if self.exec_() else None


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
#     ex.show()
    sys.exit(app.exec_())    


当我尝试创建并运行测试用例时,将弹出对话框。我只想知道是否为对话框设置了默认值,并且不想弹出它。

import unittest
import dialog_test
from PyQt5.Qt import QApplication
import sys


app = QApplication(sys.argv)

class Test(unittest.TestCase):


    def setUp(self):
        self.ex = dialog_test.Example()

    def tearDown(self):
        pass


    def testName(self):

        assert True == True


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

最佳答案

exec_()调用使QDialog可见,您不应调用它:

def popUp(self, meta=None):
    return (self.street1_edit.text(),
            self.street2_edit.text())

关于python - pytest-qt测试对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51487893/

相关文章:

可变元组长度的python字符串格式

python - 如何在Tkinter中以编程方式将同一操作与一组按钮相关联?

python - 如何将 URL 链接嵌入到 QLabel

python - 使用 QThread 动态创建 QImage 帧到 ffmpeg 标准输入

python - PyQt5 状态栏分隔符

python - 无法退出带有嵌入式 iPython Qtconsole 的 PyQt5 应用程序

python - 如何在 Python 中使用 Firebird 审计和跟踪服务?

python - 如何获取Python变量注解?

python - pyqt - 将图像复制到 QClipboard 崩溃程序

qt - 在 Qt 中,如何在菜单中显示键盘快捷键但禁用它们?