pytest-qt测试对话框

时间:2018-07-23 22:15:57

标签: 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()

1 个答案:

答案 0 :(得分:1)

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

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