Dialog button click in test not dismissing dialog PyQt5/pytest

huangapple go评论72阅读模式
英文:

Dialog button click in test not dismissing dialog PyQt5/pytest

问题

在运行一个启动对话框的测试时,测试无法以编程方式单击对话框上的按钮。如果我手动单击按钮,测试就会完成。此外,我不确定为什么对话框会显示(因为我看不到它是从哪个原始窗口启动的)。

Python 3.7.16,pytest 7.2.1

foo.py

  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. class Foo(QtWidgets.QWidget):
  4. def __init__(self):
  5. super().__init__()
  6. self.setupUi()
  7. def setupUi(self):
  8. self.resize(400, 200)
  9. self.button = QtWidgets.QPushButton("ClickMe")
  10. self.button.clicked.connect(self.launchDialog)
  11. layout = QtWidgets.QVBoxLayout()
  12. layout.addWidget(self.button)
  13. self.setLayout(layout)
  14. def launchDialog(self):
  15. self.dialog = FooDialog(parent=self)
  16. self.dialog.exec()
  17. class FooDialog(QtWidgets.QMessageBox):
  18. def __init__(self, parent=None):
  19. super(FooDialog, self).__init__(parent)
  20. self.setWindowTitle("foo.FooDialog")
  21. self.button = QtWidgets.QPushButton("ClickMe")
  22. self.addButton(self.button, QtWidgets.QMessageBox.ButtonRole.ActionRole)
  23. if __name__ == "__main__":
  24. app = QtWidgets.QApplication(sys.argv)
  25. foo = Foo()
  26. foo.show()
  27. sys.exit(app.exec_())

test_foo.py

  1. import pytest, time
  2. from pytestqt.qtbot import QtBot
  3. from PyQt5 import QtCore
  4. from foo import Foo
  5. @pytest.fixture
  6. def app(qtbot):
  7. foo = Foo()
  8. qtbot.addWidget(foo)
  9. return foo
  10. def test_button(app):
  11. qtbot = QtBot(app)
  12. qtbot.mouseClick(app.button, QtCore.Qt.MouseButton.LeftButton, delay=0)
  13. time.sleep(2)
  14. qtbot.mouseClick(app.dialog.button, QtCore.Qt.MouseButton.LeftButton, delay=0)

我运行测试如下:

  1. pytest -s test_foo.py

我运行了测试并期望它完成,但实际上测试挂起等待对话框上的按钮被点击。

英文:

When running a test that launches a dialog, the test is unable to programmatically click a button on the dialog. If I manually click the button, the test completes. Also I'm unsure why the dialog even shows at all (since I don't see the original window it was launched from).

Python 3.7.16, pytest 7.2.1

foo.py

  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. class Foo(QtWidgets.QWidget):
  4. def __init__(self):
  5. super().__init__()
  6. self.setupUi()
  7. def setupUi(self):
  8. self.resize(400, 200)
  9. self.button = QtWidgets.QPushButton("ClickMe")
  10. self.button.clicked.connect(self.launchDialog)
  11. layout = QtWidgets.QVBoxLayout()
  12. layout.addWidget(self.button)
  13. self.setLayout(layout)
  14. def launchDialog(self):
  15. self.dialog = FooDialog(parent=self)
  16. self.dialog.exec()
  17. class FooDialog(QtWidgets.QMessageBox):
  18. def __init__(self, parent=None):
  19. super(FooDialog, self).__init__(parent)
  20. self.setWindowTitle("foo.FooDialog")
  21. self.button = QtWidgets.QPushButton("ClickMe")
  22. self.addButton(self.button, QtWidgets.QMessageBox.ButtonRole.ActionRole)
  23. if __name__ == "__main__":
  24. app = QtWidgets.QApplication(sys.argv)
  25. foo = Foo()
  26. foo.show()
  27. sys.exit(app.exec_())

test_foo.py

  1. import pytest, time
  2. from pytestqt.qtbot import QtBot
  3. from PyQt5 import QtCore
  4. from foo import Foo
  5. @pytest.fixture
  6. def app(qtbot):
  7. foo = Foo()
  8. qtbot.addWidget(foo)
  9. return foo
  10. def test_button(app):
  11. qtbot = QtBot(app)
  12. qtbot.mouseClick(app.button, QtCore.Qt.MouseButton.LeftButton, delay=0)
  13. time.sleep(2)
  14. qtbot.mouseClick(app.dialog.button, QtCore.Qt.MouseButton.LeftButton, delay=0)

I run the test thus:

  1. pytest -s test_foo.py

I ran the test and expected it to complete, but instead the test hangs waiting for the button on the dialog to be clicked.

答案1

得分: 0

我通过模拟对话来解决了这个问题。

英文:

I worked around the problem by mocking the dialog

huangapple
  • 本文由 发表于 2023年2月7日 03:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定