gpt4 book ai didi

python - pytest-qt 测试对话框

转载 作者:行者123 更新时间:2023-12-01 09:13:05 25 4
gpt4 key购买 nike

我正在尝试构建一个对话框并为其编写一个测试用例。

这是我的对话框代码 -

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com