Reputation: 657
I am trying to simulate a QDialogButtonBox button click in a QDialog, as part of unit testing.
I have not found the solution to reach the QDialogButtonBox object in the QDialog instance. The only solution I found is to do an QDialog().accept() directly. Maybe there is a better way to do it?
The test does not work because I have an exception TypeError: 'module' object is not callable that I do not understand. What do I not understand?
This is the code:
import sys
import unittest
import threading
from PyQt5.QtWidgets import (
QApplication,
QDialog,
QDialogButtonBox,
QGridLayout,
QGroupBox,
QLabel,
QMainWindow,
QScrollArea,
QVBoxLayout,
QWidget,
)
app = QApplication(sys.argv)
class Fortest:
def dial(self):
dialog = QDialog()
buttonbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
layout = QVBoxLayout(dialog)
param_box = QGroupBox("Iterate over parameters:")
pblayout = QVBoxLayout()
pblayout.setContentsMargins(0, 0, 0, 0)
scroll = QScrollArea()
scroll.setWidgetResizable(True)
scroll.setFrameStyle(scroll.NoFrame)
scroll.setViewportMargins(0, 0, 0, 0)
pblayout.addWidget(scroll)
param_lay = QGridLayout()
wid = QWidget()
scroll.setWidget(wid)
wid.setLayout(param_lay)
param_box.setLayout(pblayout)
layout.addWidget(param_box)
layout.addWidget(buttonbox)
buttonbox.accepted.connect(dialog.accept)
buttonbox.rejected.connect(dialog.reject)
param_lay.addWidget(QLabel("iter. / database:"), 0, 0, 1, 3)
param_lay.addWidget(QLabel("iter.:"), 0, 3, 1, 2)
param_lay.setColumnStretch(2, 1)
param_lay.setColumnStretch(4, 1)
param_lay.setRowStretch(0, 0)
res = dialog.exec_()
if res != dialog.Accepted:
print("dialog not accepted")
self.a = False
else:
print("dialog accepted")
self.a = True
class Test(unittest.TestCase):
def test(self):
b = Fortest()
threading.Timer(1, self.execute_click).start()
b.dial()
self.assertEqual(b.a, True)
def execute_click(self):
w = QApplication.activeWindow()
if isinstance(w, QDialog):
w.accept()
if __name__ == "__main__":
unittest(Test().test())
Upvotes: 0
Views: 226
Reputation: 243927
You have 2 errors:
unittest.main()
or unittest.main(Fortest())
.import sys
import unittest
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (
QApplication,
QDialog,
QDialogButtonBox,
QGridLayout,
QGroupBox,
QLabel,
QMainWindow,
QScrollArea,
QVBoxLayout,
QWidget,
)
app = QApplication(sys.argv)
class Fortest:
def dial(self):
dialog = QDialog()
buttonbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
layout = QVBoxLayout(dialog)
param_box = QGroupBox("Iterate over parameters:")
pblayout = QVBoxLayout()
pblayout.setContentsMargins(0, 0, 0, 0)
scroll = QScrollArea()
scroll.setWidgetResizable(True)
scroll.setFrameStyle(scroll.NoFrame)
scroll.setViewportMargins(0, 0, 0, 0)
pblayout.addWidget(scroll)
param_lay = QGridLayout()
wid = QWidget()
scroll.setWidget(wid)
wid.setLayout(param_lay)
param_box.setLayout(pblayout)
layout.addWidget(param_box)
layout.addWidget(buttonbox)
buttonbox.accepted.connect(dialog.accept)
buttonbox.rejected.connect(dialog.reject)
param_lay.addWidget(QLabel("iter. / database:"), 0, 0, 1, 3)
param_lay.addWidget(QLabel("iter.:"), 0, 3, 1, 2)
param_lay.setColumnStretch(2, 1)
param_lay.setColumnStretch(4, 1)
param_lay.setRowStretch(0, 0)
res = dialog.exec_()
if res != dialog.Accepted:
print("dialog not accepted")
self.a = False
else:
print("dialog accepted")
self.a = True
class Test(unittest.TestCase):
def test(self):
b = Fortest()
QTimer.singleShot(1000, self.execute_click)
b.dial()
self.assertEqual(b.a, True)
def execute_click(self):
w = QApplication.activeWindow()
if isinstance(w, QDialog):
w.accept()
if __name__ == "__main__":
unittest.main()
Upvotes: 2