如何使用PYTEST和PYTEST-QT测试PYQT5 Qapplication?

发布于 2025-02-13 08:24:52 字数 1544 浏览 0 评论 0原文

我正在尝试使用Pytest-QT测试我的QTapplication,并需要一些基础知识的帮助。

给出这样的简单MWE:

# mwe.py
import sys
from PyQt5.QtWidgets import (QWidget, QToolTip,
    QPushButton, QApplication)
from PyQt5.QtGui import QFont


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()
        self.count = 0


    def initUI(self):

        QToolTip.setFont(QFont('SansSerif', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)
        btn.clicked.connect(self.pushedTheButton)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Tooltips')
        self.show()

    def pushedTheButton(self):
        print("Button pressed")
        self.count += 1


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

我将如何测试按下按钮时计数器已增加?

from mwe import Example
import pytest
import pytestqt
from pytestqt.qtbot import QtBot
import sys

from PyQt5.Qt import QApplication

def test_press_button(qtbot: QtBot):
    app = QApplication(sys.argv)
    ex = Example()

    qtbot.add_widget(ex)
    sys.exit(app.exec())
    
    # how to press the button and verify that counter is incermented? 
    

请注意,只需实例化示例,我就可以在主循环之外执行此操作,但是对于我的集成测试,我想打开并运行完整的应用程序。

提前致谢!

I am trying to test my QtApplication using pytest-qt and need some assistance with the basics.

Give a simple mwe such as this:

# mwe.py
import sys
from PyQt5.QtWidgets import (QWidget, QToolTip,
    QPushButton, QApplication)
from PyQt5.QtGui import QFont


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()
        self.count = 0


    def initUI(self):

        QToolTip.setFont(QFont('SansSerif', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)
        btn.clicked.connect(self.pushedTheButton)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Tooltips')
        self.show()

    def pushedTheButton(self):
        print("Button pressed")
        self.count += 1


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

How would I test that the counter has incremented when the button is pressed?

from mwe import Example
import pytest
import pytestqt
from pytestqt.qtbot import QtBot
import sys

from PyQt5.Qt import QApplication

def test_press_button(qtbot: QtBot):
    app = QApplication(sys.argv)
    ex = Example()

    qtbot.add_widget(ex)
    sys.exit(app.exec())
    
    # how to press the button and verify that counter is incermented? 
    

Note, I've been able to do this outside the main loop, by just instantiating the Example but for my integration testing I want to have the full application open and running.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文