Issue
I am testing a PyQt5 app using pytest and its pytest-qt addon.
I created a fixture to load my application class that I then use for all the test
I have two issues so far
- The gui shows up and I don't want.
- If I write more tests I will eventually get a segmentation fault as (I think) there are too many gui open.
any idea? the documentation of pytest-qt is pretty basic and actually has a test that shows the GUI
a minimanl example is:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QCoreApplication, QObject, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
class Example(QMainWindow):
def __init__(self, parent=None):
super().__init__()
self.initUI(self)
def initUI(self, MainWindow):
# centralwidget
MainWindow.resize(346, 193)
self.centralwidget = QtWidgets.QWidget(MainWindow)
# The Action to quit
self.toolb_action_Exit = QAction(QIcon("exit.png"), "Exit", self)
self.toolb_action_Exit.setShortcut("Ctrl+Q")
self.toolb_action_Exit.triggered.connect(self.close)
# The Button
self.btn_prt = QtWidgets.QPushButton(self.centralwidget)
self.btn_prt.setGeometry(QtCore.QRect(120, 20, 89, 25))
self.btn_prt.clicked.connect(lambda: self.doPrint())
self.btn_quit = QtWidgets.QPushButton(self.centralwidget)
self.btn_quit.setGeometry(QtCore.QRect(220, 20, 89, 25))
self.btn_quit.clicked.connect(lambda: self.close())
# The textEdit
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 60, 321, 81))
# Show the frame
MainWindow.setCentralWidget(self.centralwidget)
# self.show()
def doPrint(self):
print("TEST doPrint")
def closeEvent(self, event):
# Ask a question before to quit.
self.replyClosing = QMessageBox.question(
self,
"Message",
"Are you sure to quit?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No,
)
if self.replyClosing == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main_GUI():
app = QApplication(sys.argv)
imageViewer = Example()
imageViewer.show()
return app, imageViewer
if __name__ == "__main__":
app, imageViewer = main_GUI()
rc = app.exec_()
print("App end is exit code {}".format(rc))
sys.exit(rc)
and for the test:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import pytest
from PyQt5 import QtCore, QtGui, QtTest, QtWidgets
from PyQt5.QtCore import QCoreApplication, QObject, Qt
from PyQt5.QtWidgets import *
from pytestqt.plugin import QtBot
GUI = __import__("GUI")
@pytest.fixture(scope="module")
def qtbot_session(qapp, request):
print(" SETUP qtbot")
result = QtBot(qapp)
with capture_exceptions() as exceptions:
yield result
print(" TEARDOWN qtbot")
@pytest.fixture(scope="module")
def Viewer(request):
print(" SETUP GUI")
app, imageViewer = GUI.main_GUI()
qtbotbis = QtBot(app)
QtTest.QTest.qWait(0.5 * 1000)
return app, imageViewer, qtbotbis
def test_interface(Viewer):
print(" beginning ")
app, imageViewer, qtbot = Viewer
assert imageViewer.textEdit.toPlainText() == ''
Solution
If you don't want to show the GUI then a possible solution is to use the Qt::WA_DontShowOnScreen
flag, for this you must do the following:
- Remove
imageViewer.show()
in main_GUI. - Add
imageViewer.setAttribute(Qt.WA_DontShowOnScreen, True)
andimageViewer.show()
in Viewer fixture.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.