【问题标题】:How can I display GUI while testing PyQt5 app with pytest-qt?如何在使用 pytest-qt 测试 PyQt5 应用程序时显示 GUI?
【发布时间】:2018-05-27 21:28:40
【问题描述】:

我是PyQt 的新手,但我将使用pytestpytest-qt 插件来测试我的PyQt5 应用程序。我有一些使用SWTBotRCPTT 的Java GUI 测试经验,在那里我可以实时看到在测试期间控件和整个GUI 会发生什么。我想用我的新 python 工具有这样的行为,但似乎pytest-qt 以某种背景方式测试 GUI。所有代码都按我的预期工作,但在测试期间我看不到 GUI。代码很简单,就像教程中一样:

from tests.test import MyApp
from time import sleep
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


def test_myapp(qtbot):
    app = QApplication([])
    window = MyApp()
    # window.show()
    # app.exec_()
    qtbot.addWidget(window)
    qtbot.mouseClick(window.buttonBox.buttons()[0], Qt.LeftButton)
    sleep(5)
    assert window.label.text() == 'accept'

如果我取消注释 window.show() 行(他们在 tutorial 中这样做),我会看到一个奇怪的窗口,其中包含冻结的背景:

我想理论上可以显示界面,因为我知道 PyQt5 在 python shell (more) 中工作:

例如,您可以从 Python shell 提示符创建小部件,与它们交互,并且仍然能够输入其他 Python 命令

但是不知道怎么用pytest-qt来实现

【问题讨论】:

    标签: python user-interface pyqt5 pytest


    【解决方案1】:

    此代码按我的要求工作,它正确显示了界面。关键是qtbot.waitForWindowShown(window) 行。

    from tests.test import MyApp
    from time import sleep
    from PyQt5.QtCore import *
    
    
    def test_myapp(qtbot):
        window = MyApp()
        qtbot.addWidget(window)
        window.show()
        qtbot.waitForWindowShown(window)
        sleep(3)
        qtbot.mouseClick(window.buttonBox.buttons()[0], Qt.LeftButton)
        assert window.label.text() == 'accept'
        qtbot.stopForInteraction()
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2022-01-12
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多