pytest-dev / pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
https://pytest-qt.readthedocs.io
MIT License
408 stars 70 forks source link

How to test the following code...? #526

Closed hwy1992129 closed 1 year ago

hwy1992129 commented 1 year ago

It seems that exec block the test...? It stops in the sub window. The sub sub window is shown only when I close the sub window or click the button in sub window. What I expect to see in the test: the main window is shown the button in main window is clicked the sub window is shown due to the button in the main window is clicked the button in sub window is clicked the sub sub window is shown.....

from PyQt6.QtWidgets import QMainWindow, QPushButton, QDialog, QVBoxLayout, QLabel
from PyQt6.QtWidgets import QApplication
import sys

class SubSubWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("SubSubWindow")

class SubWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("SubWindow")
        self.button = QPushButton("Open SubSubWindow", self)
        self.button.clicked.connect(self.open_subsubwindow)

    def open_subsubwindow(self):
        self.sub_sub_window = SubSubWindow()
        self.sub_sub_window.exec()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("MainWindow")
        self.button = QPushButton("Open SubWindow", self)
        self.setCentralWidget(self.button)
        self.button.clicked.connect(self.open_subwindow)

    def open_subwindow(self):
        self.sub_window = SubWindow()
        self.sub_window.exec()

if __name__ == "__main__":
    app = QApplication([])

    window = MainWindow()
    window.show()
    sys.exit(app.exec())
The-Compiler commented 1 year ago

This is expected. If you launch a nested event loop, the control will be passed to that and there is no way the test can continue. See the note in the docs about testing QDialog for how to handle this.