python - 如何使用pytest-qt点击QMessageBox?

标签 python pyqt pyqt5 pytest pytest-qt

我正在使用 pytest-qtPyQt 应用程序创建一些单元测试。 我想创建打开的图形窗口,进行一些测试然后关闭窗口,而不是为每个测试打开一个新窗口,即。对窗口本身使用模块固定装置。 我成功地完成了这部分,通过在本地函数中调用 QtBot 而不是使用默认的固定装置,并删除模拟装置。所以我已经非常接近我的目标了。

但是,我无法关闭窗口(并测试 QMessageBox 的关闭事件)。

我红色的例子就像 how to handle modal dialog及其 git讨论,或qmessage问题;这似乎与我的问题很接近。 建议使用计时器等待 QMessageBox 出现,然后单击按钮选择,但显然我无法正确应用它们。 在我的尝试中,pytest 获得了结束需求,但没有单击对话框 框。所以,我必须点击自己来完成测试。

这是一个小示例,其中包含文件 GUI.py:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys

from PyQt5 import QtGui, QtCore, QtWidgets

from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, Qt, QObject
from PyQt5.QtGui import QIcon


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()
    return app, imageViewer


if __name__ == '__main__':
    app, imageViewer =main_GUI()
    rc= app.exec_()
    print('App end is exit code {}'.format(rc))
    sys.exit(rc)

和名为 test_GUI.pypytest 文件:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os, sys
import pytest

from PyQt5 import QtGui, QtCore, QtWidgets, QtTest
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, Qt, QObject

from pytestqt.plugin import QtBot

GUI = __import__('GUI')


@pytest.yield_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)
    # qtbotbis.addWidget(imageViewer)
    # qtbotbis.wait_for_window_shown(imageViewer)
    QtTest.QTest.qWait(0.5 *1000)
    yield app, imageViewer, qtbotbis

    # EXIT
    # mocker.patch.object(QMessageBox, 'question', return_value=QMessageBox.Yes)
    # imageViewer.toolb_action_Exit.trigger()
    def handle_dialog():
        # while not imageViewer.replyClosing.isVisible():
        #   app.processEvents()
        box = QMessageBox()
        box.setStandardButtons(QMessageBox.Yes)
        button = box.button(QMessageBox.Yes)
        qtbotbis.mouseClick(button, QtCore.Qt.LeftButton)
    QtCore.QTimer.singleShot(100, handle_dialog)
    qtbotbis.mouseClick(imageViewer.btn_quit, QtCore.Qt.LeftButton, delay=1)
    assert imageViewer.close()
    print("  TEARDOWN GUI")


class Test_GUI() :
    def test_interface(self, Viewer):
        print("  beginning ")
        app, imageViewer, qtbot = Viewer
        qtbot.mouseClick( imageViewer.btn_prt, QtCore.Qt.LeftButton )
        QtTest.QTest.qWait(0.5 *1000)
        assert True
        print(" Test passed")

知道我缺少什么吗?任何其他想法或建议也将不胜感激。

最佳答案

在您的尝试中,您正在创建一个新的QMessageBox,它与使用静态方法QMessageBox::question()创建的不同,因此即使您单击它也会不工作。

这个想法是获取显示的QMessageBox,在本例中我们将利用它,因为它是事件窗口,因此我们可以使用QApplication::activeWindow()获取它。另一种获取QMessageBox的方法是通过findChild()利用imageViewer和QMessageBox之间的关系:

@pytest.fixture(scope="module")
def Viewer(request):
    print("  SETUP GUI")

    app, imageViewer = GUI.main_GUI()
    qtbotbis = QtBot(app)
    QtTest.QTest.qWait(0.5 * 1000)

    yield app, imageViewer, qtbotbis

    def handle_dialog():
        messagebox = QtWidgets.QApplication.activeWindow()
        # or
        # messagebox = imageViewer.findChild(QtWidgets.QMessageBox)
        yes_button = messagebox.button(QtWidgets.QMessageBox.Yes)
        qtbotbis.mouseClick(yes_button, QtCore.Qt.LeftButton, delay=1)

    QtCore.QTimer.singleShot(100, handle_dialog)
    qtbotbis.mouseClick(imageViewer.btn_quit, QtCore.Qt.LeftButton, delay=1)
    assert imageViewer.isHidden()

关于python - 如何使用pytest-qt点击QMessageBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59147908/

相关文章:

python - 通过 for 循环调用函数,并将函数作为字符串列表

python - PyQt5 连接在这种情况下不起作用 : item cannot be converted to PyQt5. QtCore.QObject

python - 使用 PyQt 根据角度裁剪图像

python - 将 pandas 连接的列后缀转换为 MultiIndex

python - 如何提取标签内的 unicode 文本?

python - 为 3D 形状着色

python - 通过拖动 pyqt5 中的边缘来调整自定义小部件的大小

python - python中多列的索引匹配

pyqt - 设置包含QTableWidget的对话框窗口的最佳大小

python - 如何在PyQt中实现一个简单的按钮