>

프로젝트에서 모듈을 테스트하고 싶지만 문제가 있습니다. 버튼을 클릭 한 후 창을 제대로 여는 방법을 모르겠습니다.

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)

B. AddSchoolBookWindow

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()

이것은 전체 코드의 일부이지만 전체 코드는이 문제로 해결되지 않습니다.


  • 답변 # 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인지 확인해야하며, 테스트의 경우 QTest.qWait ()를 사용하는 경우 time.sleep ()을 사용하지 마십시오.

    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()
    
    

관련 자료

  • 이전 c# - 크로스 플랫폼 시스템 상태 얻기 ASPNet Core
  • 다음 node.js - 사투리 개체로 인해 순차 마이그레이션 실패 개체 지원되지 않는 오류