在pytest-qt中点击按钮后如何检查正确打开window
How to check correct opening the window after the click by button in pytest-qt
我想在我的项目中测试一个模块,但我遇到了一些问题。我不知道如何在单击按钮后检查 window 是否正确打开。
我有 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.选择AddingBookTypeWindow
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)
乙。添加教科书窗口
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()
这是完整代码的一小部分,但完整代码并未解决此问题。
你的问题是小部件不可访问,因为它是一个局部变量,我也怀疑 window 会显示,因为它应该稍后被删除,所以要访问它我将创建一个 属性:
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并且它的基础class是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()
我想在我的项目中测试一个模块,但我遇到了一些问题。我不知道如何在单击按钮后检查 window 是否正确打开。
我有 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.选择AddingBookTypeWindow
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)
乙。添加教科书窗口
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()
这是完整代码的一小部分,但完整代码并未解决此问题。
你的问题是小部件不可访问,因为它是一个局部变量,我也怀疑 window 会显示,因为它应该稍后被删除,所以要访问它我将创建一个 属性:
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并且它的基础class是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()