如何在pytest-qt中单击按钮后检查正确打开窗口

Posted

技术标签:

【中文标题】如何在pytest-qt中单击按钮后检查正确打开窗口【英文标题】:How to check correct opening the window after the click by button in pytest-qt 【发布时间】:2019-07-16 21:34:02 【问题描述】:

我想在我的项目中测试一个模块,但我遇到了一些问题。我不知道如何检查按钮单击后窗口的正确打开方式。

我有 Windows SelectAddingBookTypeWindow 和 AddSchoolBookWindow。 我想检查在单击名为 selectBtn 的按钮后是否会打开 AddSchoolBookWindow。

我当前的测试(我对最后一个字符串有问题,因为我不知道我必须使用哪种方法):

@pytest.fixture
def open_window(qtbot):
    def _press_button(window):
        widget = window()
        qtbot.addWidget(widget)
        widget.show()
        qtbot.wait_for_window_shown(widget)
        sleep(3)
        return widget
    return _press_button


class TestSelectAddingBookTypeWindow:
    def test_select_schoolbook(self, open_window, qtbot):
        widget = open_window(SelectAddingBookTypeWindow)
        qtbot.mouseClick(widget.schoolbookSelector, QtCore.Qt.LeftButton)
        qtbot.mouseClick(widget.selectBtn, QtCore.Qt.LeftButton)
        assert widget.close()
        assert AddSchoolBookWindow.isActiveWindow()

我的课程: A. SelectAddingBookTypeWindow

class SelectBookTypeWindow(QDialog, Ui_selectBookTypeWindow):
    @logger.catch
    def __init__(self, widget1, widget2):
        super().__init__()
        self.setupUi(self)
        self.selectBtn.clicked.connect(lambda x: self.open_new_window(widget1, widget2))

    @logger.catch
    def open_new_window(self, schoolbook_widget, fictionbook_widget):
        if self.schoolbookSelector.isChecked():
            widget = schoolbook_widget()
        elif self.fictionbookSelector.isChecked():
            widget = fictionbook_widget()
        widget.show()
        self.close()


class SelectAddingBookTypeWindow(SelectBookTypeWindow):
    @logger.catch
    def __init__(self):
        super().__init__(AddSchoolBookWindow, AddFictionBookWindow)

B.添加SchoolBookWindow

class AddSchoolBookWindow(QDialog, Ui_AddSchoolbookWindow):
    @logger.catch
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.widgets = [
            self.schoolbookAuthor, self.schoolbookTitle, self.schoolbookPublishHouse,
            self.schoolbookPublishYear, self.schoolbookInvoiceNum, self.schoolbookInvoiceDate, self.schoolbookClass,
            self.schoolbookCount, self.schoolbookPrice
        ]
        self.schoolbookSaveBtn.clicked.connect(lambda _: self.save_book())
        self.schoolbookClearBtn.clicked.connect(lambda _: Utils.clear_fields(self.widgets, self.schoolbookInvoiceDate))
        self.schoolbookCancelBtn.clicked.connect(lambda _: self.close())

    @logger.catch
    def save_book(self):
        field_names = Config.COLUMNS['SchoolBook']
        user_data = list(map(lambda x: x.text(), self.widgets))
        book_sum = Utils.calculate_book_sum(user_data)
        user_data.append(str(book_sum))
        book = dict(zip(field_names[1:], user_data))
        db = DatabaseManager(Config.PATHES['TO_DATABASE']['PROD'])
        model = Schoolbook('SchoolBook', db.connection)
        model.insert_book(book)
        logger.success(f'The book success add in the database')
        model.model.select()

这是完整代码的一小部分,但完整代码并不能解决这个问题。

【问题讨论】:

分享 SelectAddingBookTypeWindow 和 AddSchoolBookWindow 类,提供minimal reproducible example 我希望我可以在 (***.com/questions/13850240/… 中的响应并使用方法 isVisible() 我可以解决我的问题。我尝试在类 SelectBookTypeWindow 中名为 open_new_window 的方法中添加一个字符串return widget但我的测试字符串 qtbot.mouseClick(widget.selectBtn, QtCore.Qt.LeftButton) 返回 None。 【参考方案1】:

您的问题是小部件不可访问,因为它是一个局部变量,我也怀疑窗口是否会显示,因为它应该稍后被删除,所以要访问它,我将创建一个属性:

class SelectBookTypeWindow(QtWidgets.QDialog, Ui_selectBookTypeWindow):
    def __init__(self, widget1, widget2):
        super().__init__()
        self.setupUi(self)
        self.selectBtn.clicked.connect(lambda x: self.open_new_window(widget1, widget2))

        self._selected_window = None

    @property
    def selected_window(self):
        return self._selected_window

    def open_new_window(self, schoolbook_widget, fictionbook_widget):
        if self.schoolbookSelector.isChecked():
            self._selected_window = schoolbook_widget()
        elif self.fictionbookSelector.isChecked():
            self._selected_window = fictionbook_widget()
        if self.selected_window is not None:
            self.selected_window.show()
            self.close()

然后在测试中你应该验证 selected_window 不是 None 并且它的基类是 AddSchoolBookWindow,还要避免使用 time.sleep(),在测试的情况下使用QTest.qWait()

import pytest
from PyQt5 import QtCore, QtTest

from your_package import AddSchoolBookWindow, SelectAddingBookTypeWindow


@pytest.fixture
def open_window(qtbot):
    def callback(window):
        widget = window()
        qtbot.addWidget(widget)
        widget.show()
        qtbot.wait_for_window_shown(widget)
        QtTest.QTest.qWait(3 * 1000)
        return widget

    return callback


class TestSelectAddingBookTypeWindow:
    def test_select_schoolbook(self, open_window, qtbot):
        widget = open_window(SelectAddingBookTypeWindow)
        assert widget.isVisible()
        qtbot.mouseClick(widget.schoolbookSelector, QtCore.Qt.LeftButton)
        qtbot.mouseClick(widget.selectBtn, QtCore.Qt.LeftButton)
        assert widget.isHidden()
        assert widget.selected_window is not None
        assert isinstance(widget.selected_window, AddSchoolBookWindow)
        assert widget.selected_window.isVisible()

【讨论】:

完成。我还有一个小问题。此建议适用于在主窗口中测试工具栏元素吗? @dgonkaries 应该可以,我没测试过,试试看告诉我行不行。

以上是关于如何在pytest-qt中单击按钮后检查正确打开窗口的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 pytest-qt 鼠标单击在 QTableWidget 中选择一个项目?

单击检查按钮时如何删除/删除按钮文本?

单击登录后如何重定向到下一页并检查用户凭据是不是正确

如何在Flask中选择按钮后正确触发random.choice()?

`pytest-qt`函数`mouseMove()`不工作

单击按钮后如何检查连接互联网[重复]